Re: [xml] save a part of the tree maitaining the hierarchy from root.



Senthil Nathan wrote:
Hi All,
I would like to know how to save a part of a DOM tree, by maintaining the
hierarchy from the root based on an attribute/tag.

Suppose orig.xml is as below,
<Root>
    <Parent>
         <Child1 Apply="1">
              <Value>100</Value>
         <Child2>
               <Value>0</Value>
     </Parent>
</Root>

I need to save only the nodes which has the flag "Apply" (which means that
the node's value is changed), along with the complete hierarchy in another
xml file.

new.xml would be as,
 <Root>
    <Parent>
         <Child1 Apply="1">
              <Value>100</Value>
     </Parent>
</Root>

Just I used a simple example here. Actually the original xml file will be
a large file.

After identifying the nodes needed using XPATH, I tried using the
xmlElemDump( ), xmlDocCopyNodeList( )  which gets only the sub tree, not
with the hierarchy.  Unlinking the nodes also did not work out, as its a
large file. So, please let me know, is there any libxml-tree or save api's?
or how can i get this?

Thanks
Senthil

Hi Senthil,

I'll only browbeat you about this once, but I really think the best approach is XSLT, using libxslt in conjunction with libxml. You can accomplish what you want to do with a modified identity transform that filters out all of the Child* elements that don't have the Apply attribute. Of course, you need to be able to distinguish Child2, which you do want to lose, from Parent, which you want to keep, but I assume you know how to do that with XPATH.

If you frame this question correctly on the Mulberry Tech XSLT list, showing your input and output XML (Fix the xml - the Child elements are not valid as shown) and clearly describing the conditionals to be applied, people (XSLT experts) will probably write stylesheets for you that do the job. They you test them with xsltproc until the stylesheet is what you need, then just use libxslt to apply it in your program.

Just my 2 cents.

- Rush



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