Re: [xml] Getting element content non-recursively

Caroline Middlebrook wrote:
Hi all,

Is there a way in which I can get the content of an element that has
children, without getting the content of those children also? For example if
I have something like:


    <node2>some text</node2>


And I call xmlNodeGetContent on node1, I get "some text" and some blank
characters around it. For this particular example I would want an empty
string as node1 doesn't have any text content itself.

This is actually part of a larger problem that I am trying to solve, so
there may be a better way. Basically, for my application, I have little
fragments of XML that I need to add to various elements of other XML
documents. I need to parse these fragments to ensure they are valid so I was
looking at the functions that add children to nodes but I could not find
anything that adds (and parses) a complete fragment of xml. Therefore, what
I do instead is build a new document from my fragment (which catches any
parse errors), then I iterate around all of the attributes and nodes in a
recursive manner adding them to my target. For each node, I make a new
element with the name and content of the original and here is where I get my
problem - the xmlNodeGetContent function is giving me the concatenated
content of all children but as I am handling recursion myself, I need only
the content of the actual node I am referring to.

Hi Caroline,

This actually sounds like a job for XSLT. Have you not taken that approach for a particular reason?

- Rush

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]