tags:

views:

66

answers:

2

I am having data like below:

<ProductAttributes>
<Attribute>
  <ItemCode>ITEM-0174676</ItemCode> 
  <AttributeCode>host_interface</AttributeCode> 
  <AttributeDescription>Host Interface</AttributeDescription> 
  <AttributeValueCode>usb</AttributeValueCode> 
  <AttributeValueDescription>USB</AttributeValueDescription> 
  <GroupCode>technical_information</GroupCode> 
  <GroupDescription>Technical Information</GroupDescription> 
  <GroupPostion /> 
  <DisplayInList>True</DisplayInList> 
  <GroupPosition>1</GroupPosition> 
  </Attribute>
<Attribute>
  <ItemCode>ITEM-0174676</ItemCode> 
  <AttributeCode>host_interface</AttributeCode> 
  <AttributeDescription>Host Interface</AttributeDescription> 
  <AttributeValueCode /> 
  <AttributeValueDescription>USB</AttributeValueDescription> 
  <GroupCode>technical_information</GroupCode> 
  <GroupDescription>Technical Information</GroupDescription> 
  <GroupPostion /> 
  <DisplayInList>True</DisplayInList> 
  <GroupPosition>1</GroupPosition> 
  </Attribute>
<Attribute>

Here in above xml <AttributeDescription> is having same text in both <Attribute> node in that case i want to display the reslut as below which will use <AttributeValueDescription> node so the result would be

Host Interface: USB, USB

So any help for the result?

Thanks in advance, Om

+1  A: 

To build a string separated by a comma, you can go with this previous question: XSLT concat string, remove last comma

Rubens Farias
+2  A: 

I assume you want HTML as the output.

You need to group the data by <ItemCode>, <AttributeCode>. This means a compound Muenchian grouping approach. You need this key:

<xsl:key 
  name="AttributeByAttributeCode" 
  match="Attribute" 
  use="concat(ItemCode, '|', AttributeCode)" 
/>

Then, you can use the key to group by <AttributeCode> within each <ProductAttributes>:

<xsl:template match="ProductAttributes">
  <!-- check every attribute… -->
  <xsl:for-each select="Attribute">

    <!-- …select all attributes that share the same item and attribute codes -->
    <xsl:variable name="EqualAttributes" select="
      key('AttributeByAttributeCode', concat(ItemCode, '|', AttributeCode))
    " />

    <!-- make sure output is generated for the first of them only -->
    <xsl:if test="generate-id() = generate-id($EqualAttributes[1])">
      <div>
        <xsl:value-of select="AttributeDescription" />
        <xsl:text>: </xsl:text>
        <!-- now make a list out of any attributes that are equal -->
        <xsl:apply-templates mode="list" select="
          $EqualAttributes/AttributeValueDescription
        " />
      </div>
    </xsl:if>
  </xsl:for-each>
</xsl:template>

<!-- generic template to make a comma-separated list out of input elements -->
<xsl:template match="*" mode="list">
  <xsl:value-of select="." />
  <xsl:if test="position() &lt; last()">
    <xsl:text>, </xsl:text>
  </xsl:if>
</xsl:template>

The above would lead to

<div>Host Interface: USB, USB</div>
Tomalak
Thank you very much Tomalak it's working!!!Thank you.
Om
Glad to help. :) P.S.: You can mark the answer as accepted if you have no further questions in this regard.
Tomalak
I am new in this blog is there is some link or button to mark it as 'Answer accepted'.
Om
There is a big check mark at every answer that is impossible to overlook. ;-)
Tomalak