XSLT Help getting Distinct values
I have an XML document (shown below), what I need to do is get a list of all the unique Type @ID values, regardless of where they are below SizeRanges
So for this document I need to get 10,8,6,5 into something I can use for-each upon.
Ultimately, I'm trying to show each SizeRange in a table beside each other, and get the same Types to line up on the same row. But the problem I am having is extracting a distinct list of Types I can loop on.
XML Document:
<SizeRanges>
<SizeRange ID="1" Name="8-18">
<Types>
<Type ID="10">
<Size Name="8" Quantity="1" />
<Size Name="10" Quantity="2" />
<Size Name="12" Quantity="2" />
<Size Name="14" Quantity="3" />
<Size Name="16" Quantity="开发者_运维百科1" />
<Size Name="18" Quantity="1" />
</Type>
<Type ID="8">
<Size Name="8" Quantity="1" />
<Size Name="10" Quantity="1" />
<Size Name="12" Quantity="2" />
<Size Name="14" Quantity="2" />
<Size Name="16" Quantity="1" />
<Size Name="18" Quantity="1" />
</Type>
<Type ID="6">
<Size Name="8" Quantity="1" />
<Size Name="10" Quantity="1" />
<Size Name="12" Quantity="1" />
<Size Name="14" Quantity="1" />
<Size Name="16" Quantity="1" />
<Size Name="18" Quantity="1" />
</Type>
<Type ID="5">
<Size Name="10" Quantity="1" />
<Size Name="12" Quantity="1" />
<Size Name="14" Quantity="1" />
<Size Name="16" Quantity="1" />
<Size Name="18" Quantity="1" />
</Type>
</Types>
</SizeRange>
<SizeRange ID="2" Name="S-XL">
<Types>
<Type ID="10">
<Size Name="S" Quantity="1" />
<Size Name="M" Quantity="3" />
<Size Name="L" Quantity="4" />
<Size Name="XL" Quantity="2" />
</Type>
<Type ID="8">
<Size Name="S" Quantity="1" />
<Size Name="M" Quantity="2" />
<Size Name="L" Quantity="3" />
<Size Name="XL" Quantity="2" />
</Type>
<Type ID="6">
<Size Name="S" Quantity="1" />
<Size Name="M" Quantity="2" />
<Size Name="L" Quantity="2" />
<Size Name="XL" Quantity="1" />
</Type>
<Type ID="5">
<Size Name="S" Quantity="1" />
<Size Name="M" Quantity="1" />
<Size Name="L" Quantity="2" />
<Size Name="XL" Quantity="1" />
</Type>
</Types>
</SizeRange>
</SizeRanges>
Give this a shot:
<xsl:key name="types" match="Type" use="@ID"/>
<xsl:variable name="distinctTypes" as="xs:integer*" select="distinct-values(//SizeRange/Types/Type)" />
<xsl:template match="/">
<xsl:for-each select="$distinctTypes">
<xsl:value-of select="." />
</xsl:for-each>
</xsl:template>
Here's another way:
<xsl:key name="types" match="Type" use="@ID"/>
<xsl:template match="/">
<xsl:for-each select="//SizeRange/Types/Type[generate-id() = generate-id(key('types', @ID)[1])]">
<xsl:value-of select="@ID"/>
</xsl:for-each>
</xsl:template>
on XSLT 2.0, you can do this way..
distinct-values(/SizeRanges/SizeRange/Types/Type/@ID)
This gives unique ID's as output
精彩评论