Reputation: 8203
I have the following sample XML:
<block>
<see link="IDNUMBER-1">(<i>See:</i> IDNUMBER-1</see>; <see link="IDNUMBER-2"> IDNUMBER-2)</see>
<see link="IDNUMBER-3">(<i>See:</i> IDNUMBER-3)</see>
</block>
I am supposed to link these <see>
elements to their respective pages, but only the IDNUMBER text - in other words, the "See:" part as well as the paranthesis should not be clickable at all.
The good part is that the link
attribute is always the same as the text that needs to be clickable.
So far I have this code:
<xsl:template match="see">
<a>
<xsl:attribute name="href">
<xsl:value-of select="concat('#', @link)"/>
</xsl:attribute>
<xsl:apply-templates/>
</a>
</xsl:template>
And while this is "correct", it renders the whole thing clickable. Any way I could somehow instruct XSLT to exclude the "See:" part, or make it so that it initially makes only the link
attribute (that is, its relevant text) clickable?
EDIT: XSLT version 1.1
Upvotes: 0
Views: 44
Reputation: 70648
What you could do, instead of matching on see, match on its text nodes, but only where the text contains the link attribute
<xsl:template match="see/text()[contains(., ../@link)]">
This means it won't match text nodes that just contain a single parenthesis, for example.
However, it will obviously match the text node "IDNUMBER-2)", but you could use the substring-before and substring-after functions to output these either side of the link.
Try this XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes" />
<xsl:template match="see/text()[contains(., ../@link)]">
<xsl:value-of select="substring-before(., ../@link)" />
<a href="#{../@link}">
<xsl:value-of select="../@link" />
</a>
<xsl:value-of select="substring-after(., ../@link)" />
</xsl:template>
</xsl:stylesheet>
Note the use of Attribute Value Templates in outputing the href attribute here. The curly braces indicate an expression to be evaluated, rather than output literally, and makes the code much more concise and readable.
Upvotes: 1