tags:

views:

227

answers:

1

My XML looks like this

<page>   
 <orderhistory>   
  <order>   
   <ordernr>10610000000001</ordernr>    
     <orderrecord>   
        <productcategory>Something</productcategory> 
     </orderrecord>   
     <orderrecord>   
        <productcategory>Something</productcategory> 
     </orderrecord>   
     <orderrecord>   
        <productcategory>Something</productcategory> 
    </orderrecord>   
   </order>   
   <order>   
    <ordernr>10210000000001</ordernr>    
     <orderrecord>   
       <productcategory>Something</productcategory> 
     </orderrecord>   
  </order>   
  <order>   
 <ordernr>10110000000001</ordernr>    
 <orderrecord>   
 <productcategory>Something</productcategory> 
  </orderrecord>   
  </order>   
  <order>   
   <ordernr>10310000000001</ordernr>    
   <orderrecord>   
   <productcategory>Something</productcategory> 
   </orderrecord>   
   <orderrecord>   
      <productcategory>Forms</productcategory> 
   </orderrecord>   
  </order>   
</orderhistory>

What i want is to show all productcategories that belongs to a single order. so if there are more orderrecords in one order I want to show all categories without duplicates next the ordernr.

But it looked like this

03-06-2009 10610000000001 something  something
03-06-2009 10210000000001 something
03-06-2009 10110000000001 something
03-05-2009 10310000000001 Forms something

I made a XSL to group the productcategories from the orderrecords, so that duplicates are removed. It seemed to work fine, but the problem is that i want it to be grouped by order.

<xsl:key name="orderrecord-by-productcategory" match="orderrecord" use="productcategory" />
<xsl:template match="/page/orderhistory/order"> 
<xsl:for-each select="orderrecord[generate-id(.) =generate-id(key('artists-by-country', productcategory)[1])]" >
<xsl:value-of select="productcategory" />
</xsl:for-each>
</xsl:template>

My output looks like this now

03-06-2009 10610000000001 something 
03-06-2009 10210000000001 
03-06-2009 10110000000001 
03-05-2009 10310000000001  Forms

but i want it to look like this.

03-06-2009 10610000000001 something
03-06-2009 10210000000001 something
03-06-2009 10110000000001 something
03-05-2009 10310000000001 Forms something

How can i do this?

+2  A: 

I think one way to do this is to use a key that is a concetation of the order number and the product category so that duplicate categories for different orders are treated differently

<xsl:key name="prodcat" match="productcategory" use="concat(../../ordernr/text(), concat('-', text()))"/>

This concatenates the order number, a hyphen (probably not needed), and the product category. Your XSL could then look something like this:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    <xsl:output method="text" />
    <xsl:key name="prodcat" match="productcategory" use="concat(../../ordernr/text(), concat('-', text()))"/>
    <xsl:template match="/page/orderhistory">
     <xsl:for-each select="order">
      <xsl:value-of select="ordernr" />
      <xsl:for-each select="orderrecord/productcategory[generate-id(.) = generate-id( key( 'prodcat', concat(../../ordernr/text(), concat('-', text())) )[1] )]">
       <xsl:sort select="text()" />
       <xsl:value-of select="." />
      </xsl:for-each>
     </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

There aren't any line-breaks or spaces in the output produced, but I hope it gives you the general idea.

Tim C