How can I get the element value only without the sub-node value?
For example
XML
<root>
<a>
parent value
<b>
child value
</b>
</a>
</root>
XSL
<xsl:for-each select="a">
<xsl:call-template name="foo">
<xsl:with-param name="elem" select="." />
</xsl:call-template>
</xsl:for-each>
<xsl:template name="foo">
<xsl:param name="elem" />
<i>Val: <xsl:value-of select="$elem"/></i>
</xsl:template>
The output is: "parent valuechild value" And I want just to display "parent value"
Any suggestions?
Thanks!
CodePudding user response:
<xsl:value-of select="text()"/>
gives you the value of all child text nodes of the context node so I am not sure why you need a named template at all, I would use <xsl:value-of select="text()"/>
in the context of the a
element i.e. inside of your for-each
. value-of
takes a separator attribute which defaults to a single space.
CodePudding user response:
Use either:
<xsl:value-of select="$elem/text()"/>
or:
<xsl:value-of select="$elem/text()[1]"/>
depending on whether you want to get the value of all text nodes that are children of a
or only the first one of them.
CodePudding user response:
You've shown us one test case with the expected output for that test case, but you haven't explained the general problem: what other inputs does the stylesheet have to deal with, and what are the general rules to apply.
With only guesswork to go on, my guess would be that the right solution to this is to use template rules. Something like:
<xsl:template match="a">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="a/text()">
<xsl:value-of select="."/>
</xsl:template>
<xsl:template match="a/b"/>
but the actual set of rules depend on all the things that MIGHT be found in the source document, rather than those present in this one example.