Re: [xsl] Unable to get text() of node

Subject: Re: [xsl] Unable to get text() of node
From: "Liron" <magilam@xxxxxxxxxxxxxxxx>
Date: Sat, 21 Jan 2006 20:29:37 +0100
Hi Hugh,

Thank you very much for your reply.
Unfortunetaly "parent::child/text()" didn't work for me and returned empty nodes. The only solution offered to me that worked so far is "text()[2]", but like you said, if the text is located before the childname node or in any other position than the second one, it won't work...
I'm using libxsl to do all the xsl processing


Any ideas?

Thanks again
Liron

----- Original Message ----- From: "CyberSpace Industries 2000 Inc." <csi2000@xxxxxxxxxxxxxxx>
To: <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
Sent: Saturday, January 21, 2006 6:29 PM
Subject: Re: [xsl] Unable to get text() of node



You might try this..

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="Untitled2.xslt"?>
<tree>
<child>First Child</child>
<child>
 <childname>SecondChild</childname>
     Second Child
  </child>
<child>
 <childname>ThirdChild</childname>
     Third Child
  </child>
</tree>

results in the following in memory tree of nodes (ignoring white space nodes)

/
|
tree
|
------------------------------------
| | |
child child child
| | |
text() ------- ---------
First Child | | | |
childname text() childname text()
| Second Child | Third Child
text() text()
SecondChild ThirdChild


and this xslt seems to get what you want...

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<tree>
<xsl:apply-templates/>
</tree>
</xsl:template>


<xsl:template match="child">
    <!-- process child nodes named childname only-->
 <xsl:apply-templates select="childname"/>
</xsl:template>

<xsl:template match="childname">
<childtext>
<!--now that we know there is a childname - get the text node of the parent named child
<xsl:value-of select="parent::child/text()"/>
</childtext>
</xsl:template>


</xsl:stylesheet>

which gives the following output...

<?xml version="1.0" encoding="UTF-8"?>
<tree>
<childtext>
     Second Child
  </childtext>
<childtext>
     Third Child
  </childtext>
</tree>

The XPath statement in the childname template starts at the current node in the tree (childname) - then goes along Parent axis to an element named child (parent::child), then goes down to a child text node (/text()). I find I have to keep this in memory tree of nodes clearly in my mind to work out appropriate Xpaths.

Incidentally - when I tried your original xslt (Xpath was <xsl:value-of select="../text()"/> in XML Spy - which is a short hand way of writing the XPath - it gives me the correct answer as well (not empty elements). What processor are you using?

This assumes there is only one childname per child though. If there can be more than one childtext elements per child,
then the select statement in the childtext tempalte will return a node set, and all nodes except the first will be ignored. You will get repeated childtext nodes in that case.


Note: text()[2] will only work if the text you want is the second node and will fail if the text appears before the childname element.

Also note that even if childname is "empty" - as long as the childname node exists - you will get the text node of child output. I suppose if you wanted otherwise you would have to test to see if the childname node has a text node as a child.



Hope this is helpful...

Cheers....Hugh
CyberSpace Industries 2000 Inc.
XML Training and Consulting

Current Thread