XSLT Match Node Not Value
The following XSLT fragment is used to wrap/tag the first and last text
nodes with a given @audience attribute for highlighting.
<xsl:template match="text()">
<xsl:if test=". =
((((ancestor::*[contains(@audience,'FLAG_')])[last()])/descendant::text())[1])">
<xsl:call-template name="flagText"/>
</xsl:if>
<xsl-value-of select="."/>
<xsl:if test=". =
((((ancestor::*[contains(@audience,'FLAG_')])[last()])/descendant::text())[last()])">
<xsl:call-template name="flagText"/>
</xsl:if>
</xsl:template>
Pseudo-code:
Find the last (nearest) ancestor element that matches the flag criteria
and then find the first and last text nodes that are descendants of that
element and flag them.
The logic is correct but the implementation is wrong. This does indeed
find the first and last text nodes but it is matching the values rather
than the node. This is flagging any text node that has the same value as
the first or last nodes.
Example:
The quick brown fox jumped over the lazy dog.
Current output:
[FLAG]The quick brown fox jumped over [FLAG]the lazy dog[FLAG].
The [1] and dog [last()] are properly flagged but it's also catching the
word 'the' in the middle due to the string matching or being equal to the
first.
Edit:
Expected (desired) output:
[FLAG]The quick brown fox jumped over the lazy dog.[FLAG]
How can I reorganize my statement to only match the first and last nodes?
I don't want to compare the strings I just want to select the first and
last.
Thanks.
No comments:
Post a Comment