0

I'm currently struggling with a really weird problem. Ive got an XML which looks like this:

<A>
  <B>
    <C>
      <D>
       <E attribute="foo">
         This is a value
       </E>
      </D>
    </C>
  </B>
</A>

Now when I'm executing my XPath-Query and traverse the result, I'm always getting the value "This is a value" as value of node A, which is not correct (The value should only be node E's value).

What is going wrong here? Seems like when saying A->childNodes, it gets all childNodes recursively and overwrites the values.

Any help would greatly be appreciated :)

2
  • Are you echoing this to a browser, by any chance? Try viewing the source and you will see why you are seeing this behaviour... ;-) Commented Aug 14, 2012 at 8:35
  • How do you traverse through your XML? DOM? Your XPath-query please. Commented Aug 14, 2012 at 9:15

1 Answer 1

1

The handler for $node->nodeValue is dom_node_node_value_read which is impementated in ext/dom/node.c.
It calls libxml2's xmlNodeGetContent() function.

Read the value of a node, this can be either the text carried directly by this node if it's a TEXT node or the aggregate string of the values carried by this node child's (TEXT and ENTITY_REF). Entity references are substituted.
I.e. for a DOMElement you get all the values of all the child elements as one string.
That's not what http://www.w3.org/TR/REC-DOM-Level-1/level-one-core.html#ID-1841493061 says if I'm not mistaken... but that's how it is implemented in libxml2/php

Sign up to request clarification or add additional context in comments.

1 Comment

Interesting, I'd assumed the problem was that it was effectively calling $node->C14N() and returning entire the content tags included, and the browser was then eating the tags when rendering. But it sound like libxml does something, er, unexpected here. Looks like you'd have to check $node instanceof DOMText in order for this to be "safe".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.