Reputation: 113
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
Upvotes: 0
Views: 434
Reputation: 338208
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() < last()">
<xsl:text>, </xsl:text>
</xsl:if>
</xsl:template>
The above would lead to
<div>Host Interface: USB, USB</div>
Upvotes: 2
Reputation: 57946
To build a string separated by a comma, you can go with this previous question: XSLT concat string, remove last comma
Upvotes: 1