xsl-list
[Top] [All Lists]

Re: [xsl] grouping with xslt 1.0

2009-09-23 06:11:16
Garvin Riensche wrote:

Here is my input:

<data>
   <product id="1">
       <seta numer="5">
       ...
       </seta>
       <seta number="9">
           <element>
               <name>A</name>
               <properties>...</properties>
           </element>
       </seta>
       <setc>....</setc>
   </product>
   <product id="2">
       <seta numer="5">
       ...
       </seta>
       <seta number="9">
           <element>
               <name>B</name>
               <properties>...</properties>
           </element>
       </seta>
       <setc>....</setc>
   </product>
   <product id="3">
       <seta numer="5">
       ...
       </seta>
       <seta number=9>
           <element>
               <name>A</name>
               <properties>...</properties>
           </element>
       </seta>
       <setc>....</setc>
   </product>
</data>

There are three "product" elements which hold "seta" and "setc" tags. Within the "seta" tag there is a name by which the products should be grouped. All products with the same name hold the same "seta" elements but different "setc" elements. So I want to create for both "product" elements with the same name (A) a new element "konto" with attribute "name=A". The konto element holds the "seta" elements which are identical or "A" and "B" and the two(!) "setc" elements. One setc from A and one from B.

The result should look like this:
<data>
   <konto name="A">
       <seta number="5">
       ...
       </seta>
       <seta number=9>
           <element>
               <name>A</name>
               <properties>...</properties>
           </element>
       </seta>
       <setc>...</setc>
       <setc>...</setc>
   </konto>
   <konto name="B">
       <seta numer="5">
       ...
       </seta>
       <seta number=9>
           <element>
               <name>B</name>
               <properties>...</properties>
           </element>
       </seta>
       <setc>....</setc>
   </konto>
</data>

Here is an example using Muenchian grouping. For the input above it produces the output described above:

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  version="1.0">

  <xsl:strip-space elements="*"/>
  <xsl:output indent="yes"/>

  <xsl:key name="k1" match="product" use="seta/element/name"/>

  <xsl:template match="data">
    <xsl:copy>
<xsl:apply-templates select="product[generate-id() = generate-id(key('k1', seta/element/name)[1])]"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="product">
    <konto name="{seta/element/name}">
      <xsl:copy-of select="seta"/>
      <xsl:copy-of select="key('k1', seta/element/name)/setc"/>
    </konto>
  </xsl:template>

</xsl:stylesheet>
--

        Martin Honnen
        http://msmvps.com/blogs/martin_honnen/

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>