I have an XSLT in which I create (from the input data) intermediary variables like the following (hard-coded example, but dynamic in nature):
<xsl:variable name="variableX">
<ValidCode CodePart="CP1" Code="C1"/>
<ValidCode CodePart="CP2" Code="C2"/>
<ValidCode CodePart="CP1" Code="C3"/>
<ValidCode CodePart="CP2" Code="C4"/>
<ValidCode CodePart="CP2" Code="C5"/>
</xsl:variable>
I wish to loop over the distinct occurrences of CodePart values. In XSLT 2.0 it’s easy:
<xsl:for-each select="distinct-values($variableX/ValidCode/@CodePart)">...</xsl:for-each>
But how best do this in XSLT 1.0?
Note that I can’t use a key, as it’s a dynamically determined variable and not part of the input file.
My input file does contain a list of all possible code parts as follows:
<root>
<CodePart><value>CP1</value></CodePart>
<CodePart><value>CP2</value></CodePart>
<CodePart><value>CP3</value></CodePart>
</root>
So I thought of looping over //CodePart/value instead, ensuring uniqueness for starters. But then I need some Xpath expression that includes the condition
“value occurs in the node-set of all $variableX/ValidCode/@CodePart values”
and use something like
<xsl:for-each select="//CodePart[..condition..]/value">...</xsl:for-each>
Is there a simple form of the Xpath expression I am looking for?
Or is another approach preferable?
I don’t think you can use directly an XPath – but you should be able to check for only the valid values like this:
or more simply (thanks to the comments):
If
$variableXis not a node set but an XML fragment it needs to be converted to a node set – this is implementation-dependent, using Microsoft processors: