I have an Xml file:
<?xml version="1.0" encoding="utf-8" ?>
<root>
<child>
<gc>gc1Value</gc>
</child>
<child>child2Value</child>
<child>
<gc>gc2Value</gc>
<gc>gc3Value</gc>
<gc>
<ggc>ggcValue</ggc>
<ggc>ggcValue</ggc>
</gc>
</child>
</root>
and an Xslt file:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<root>
<xsl:for-each select="root/child">
<xsl:if test=".!=''">
<value>
<xsl:value-of select="."/>
</value>
</xsl:if>
<xsl:for-each select="root/child/gc">
<xsl:if test=".!=''">
<value>
<xsl:value-of select="."/>
</value>
</xsl:if>
<xsl:for-each select="root/child/gc/ggc">
<xsl:if test=".!=''">
<value>
<xsl:value-of select="."/>
</value>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</root>
</xsl:template>
</xsl:stylesheet>
I’m expecting to see this kind of result:
<root>
<value>gc1Value</value>
<value>child2Value</value>
<value>gc2Value</value>
<value>gc3Value</value>
<value>ggcValue</value>
<value>ggcValue</value>
</root>
but I’m getting this result:
<?xml version="1.0" encoding="utf-8"?>
<root>
<value>
gc1Value
</value>
<value>child2Value</value>
<value>
gc2Value
gc3Value
ggcValue
ggcValue
</value>
</root>
I thought that by using . for the select this would only select the current node’s value, but it appears to be getting the values from the children as well. What should I do instead?
The problem is in here:
“.” will get the current node and all its children.
Use node::text() [ ie. select=”text()” ] if you only want the text. Also I would advise using ‘string-length(node) != 0’ and the likes instead of ”,