I have the following template:
<xsl:template match="footnote">
<xsl:variable name = "string">
<xsl:value-of select="."/>
</xsl:variable>
<xsl:variable name = "bool">
<xsl:if test="$string = preceding-sibling::node()/$string">
<xsl:text>false</xsl:test>
</xsl:if>
</xsl:variable>
<xsl:if test="$bool != 'false'">
<!-- DO STUFF -->
</xsl:if>
</xsl:template>
I'm trying to check the $string variable of the current node and check it against all previous footnote nodes to see if they have the same $string variable. If it doesn't match up with any of the preceding siblings then it should do stuff, otherwise it should do nothing.
With the code I have, the test "$string = preceding-sibling::node()/$string" always evaluates to true, even when no footnote nodes have been created yet.
Can anyone help me out? I'm having a hard time creating the expression to compare against the variable in all of the previous siblings.
EDIT: Sample XML:
<xml>
<footnote>Footnote 1</footnote>
<footnote>Footnote 2</footnote>
<footnote>Footnote 1</footnote>
<footnote>Footnote 1</footnote>
<footnore>Footnote 3</footenote>
</xml>
I'm trying to transform that into:
<xml>
<footnote>Footnote 1</footnote>
<footnote>Footnote 2</footnote>
<footnote>Footnore 3</footnore>
</xml
In XSLT 2.0 you have a distinct-values function:
Well. posting Your output XML made it more clear! there you go with solution:
Sample XML:
And XSL:
Result:
Assuming you want to check the value of the current footnote
text()
against previous footnotetext()'s
for uniqueness, then you can just check thepreceding-sibling::node()'s text()
:e.g.
Will turn the input:
Into:
You should also look at Muenchian grouping if your intention is to identify groups within elements.
You could use
to create a template that only fires for the first instance of a given footnote string. The
. = preceding-sibling::footnote
is true if the string value of this node is the same as that of any of its preceding siblings, sonot(....)
will be true if it is different from all of them.But for large documents this will be rather inefficient (quadratic in the number of footnote elements). Better would be to use
<xsl:for-each-group>
(XSLT 2.0) or Muenchian Grouping (XSLT 1.0) to group thefootnote
elements by their string value and extract the first element from each group.