Reputation: 99
I have the following input
<LOV_List>
<LOV>
<DisplayValue>...</DisplayValue>
<Code>15</Code>
</LOV>
<LOV>
<DisplayValue>...</DisplayValue>
<Code>15</Code>
</LOV>
...
<LOV_List>
I'm trying to get the following output
<List>
<values>
<Id>1</Id>
<DisplayValue1>...</DisplayValue1>
<DisplayValue2>..</DisplayValue2>
<values>
</List>
But in order to get it I need to compare the <Code>
values. If the value is the same than I need to return the DisplayValues
of the nodes.
I'm new at this so please give me pointers how to proceed.
Upvotes: 0
Views: 105
Reputation: 3247
This is a case of grouping and since you are using XSLT 1.0, you will have to use muenchian grouping to get the required output.
Define a key to group the elements using the value of Code
.
<xsl:key name="kCode" match="LOV" use="Code" />
Match the grouped elements using their ids and the key.
<xsl:template match="LOV[generate-id() = generate-id(key('kCode', Code)[1])]">
<values>
<Id><xsl:number format="1" /></Id>
<xsl:for-each select="key('kCode', Code)">
<DisplayValue>
<xsl:value-of select="DisplayValue" />
</DisplayValue>
</xsl:for-each>
</values>
</xsl:template>
The complete XSLT is as below
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="xml" />
<xsl:strip-space elements="*" />
<xsl:key name="kCode" match="LOV" use="Code" />
<xsl:template match="LOV_List">
<List>
<xsl:apply-templates />
</List>
</xsl:template>
<xsl:template match="LOV[generate-id() = generate-id(key('kCode', Code)[1])]">
<values>
<Id><xsl:number format="1" /></Id>
<xsl:for-each select="key('kCode', Code)">
<xsl:copy-of select="DisplayValue" />
</xsl:for-each>
</values>
</xsl:template>
<xsl:template match="LOV" />
</xsl:stylesheet>
Output
<List>
<values>
<Id>1</Id>
<DisplayValue>..</DisplayValue>
<DisplayValue>....</DisplayValue>
</values>
</List>
Upvotes: 1