General XMetaL Discussion

XMetaL Community Forum General XMetaL Discussion Not able to access textnodes per XPath expression

  • spyro

    Not able to access textnodes per XPath expression

    Participants 2
    Replies 3
    Last Activity 13 years ago

    Hi 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 Jung

    Reply

    tonys

    Reply to: Not able to access textnodes per XPath expression

    My 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.

    Reply

    dcramer

    Reply to: Not able to access textnodes per XPath expression

    Try

    msgbox activeDocument.DocumentElement.getNodesByXpath(“//element[2]/text()”).item(0).nodevalue

    (added // before “element” in the xpath)

    Reply

    spyro

    Reply to: Not able to access textnodes per XPath expression

    Hello 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

    Reply

  • You must be logged in to reply to this topic.

Lost Your Password?

Products
Downloads
Support