I am new to xml and xsl, if this is a easy question, and if my term is not correct, I am sorry for that.
My question is that, the “ab” and “cd” belong to element, however in xsl, the xpath expression “../text()”, always point to “ab”, but not “cd”.
Is there any way I can get “cd”? Thanks a lot!
xml:
<root>
ab
<CH>xxxx</CH>
cd
<CH>xxxx</CH>
</root>
xsl:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="root" >
<xsl:apply-templates select="CH"/>
</xsl:template>
<xsl:template match="CH" >
<c>
<xsl:value-of select="../text()"/>
</c>
</xsl:template>
</xsl:stylesheet>
current result:
<?xml version="1.0"?>
<c>
ab
</c><c>
ab
</c>
desire result:
<?xml version="1.0"?>
<c>
ab
</c><c>
cd
</c>
sorry, missing the sample code
<root>
ab
<CH>xxxx</CH>
cd
<CH>xxxx</CH>
</root>
current result: <c>ab</c><c>ab</c>
desire result: <c>ab</c><c>cd</c>
returns the two text nodes which are children of your parent.
implicitly added a
[1]to filter the select statement to the first item in the select statement. There are a number of ways to select the appropriate text node, usingpreceding-sibling::as suggested by Alejandro is probably the best for your problem, but another solution which makes it clearer what is happening with your current../text()expression would be to change the template to: