General XMetaL Discussion
XMetaL Community Forum › General XMetaL Discussion › Not able to access textnodes per XPath expression
-
spyro July 2, 2009 at 9:46 am
Not able to access textnodes per XPath expression
July 2, 2009 at 9:46 amParticipants 2Replies 3Last Activity 13 years, 7 months agoHi there,
In my current project I'm looping through the member elements of a nodeset trying to concatenate all descendant textnodes of them in a string using the node.getNodesByXpath-instruction. I can access attribut values without problems but I'm not able to read the content of the text itself.
I made a dead-simple testdocument like this:
Hello world!
What's up?” To access the text “What's up?” I use the xpath-instruction “//element[2]/text()”.
Inside a XMetaL-macro the line
msgbox activeDocument.DocumentElement.getNodesByXpath(“element[2]/text()”).item(0).nodevalue
returns a empty string instead of the expected “What's up?”. The property “length” gives me the value “0” instead of 1. What am I doing wrong?
Thanks,
Rupert Jungtonys July 5, 2009 at 11:38 pm
Reply to: Not able to access textnodes per XPath expression
July 5, 2009 at 11:38 pmMy guess is that you're seeing text nodes containing white space that you're not expecting. For example, the newline between
and will probably appear as a text node. dcramer July 6, 2009 at 2:03 am
Reply to: Not able to access textnodes per XPath expression
July 6, 2009 at 2:03 amTry
msgbox activeDocument.DocumentElement.getNodesByXpath(“//element[2]/text()”).item(0).nodevalue
(added // before “element” in the xpath)
spyro July 6, 2009 at 7:00 am
Reply to: Not able to access textnodes per XPath expression
July 6, 2009 at 7:00 amHello dcramer,
you're right, of course. I forgot this to add this to my post here in the first case (I've corrected that know). My problem is, that exactly this expression “//element[2]/text()” doesn't give me the expected result.
Furthermore I am not able to correctly address the content within element 1 with the expression “//element[1]//text()” which should deliver 2 textnodes (“Hello ” and “world!”) instead of an empty element.
spyro
-
AuthorPosts
- You must be logged in to reply to this topic.