Re: [xml] Newbie question on libxml



Hi,

I'm not 100% sure what is your intention, but I'll to try
to comment this a bit.

On Wed, 2005-11-09 at 10:11 -0800, Medi Montaseri wrote:
Being new to libXML, and I have the following question:
Say I have the following XML file representing a collection of system 
configuration files (a manifest) such as
/etc/foo.conf, /etc/goo.conf that needs to be copied, deleted, etc based 
on the operation (COPY, RESET, RESTORE).

OK

<?xml version="1.0" ?>
<!DOCTYPE object
[
        <!ELEMENT object (name, type, op+)>
        <!ELEMENT name (#PCDATA)>
        <!ELEMENT type (#PCDATA)>
        <!ELEMENT op (action, src, dst)>
        <!ELEMENT action (#PCDATA)>
        <!ELEMENT src (#PCDATA)>
        <!ELEMENT dst (#PCDATA)>
]>

<object>
        <name>/etc/foo.conf</name>
        <type>file</type>
        <op>
                <action>RESET</action>
                <src>/tmp/foo.conf.orig</src>
                <dst>/etc/foo.conf</dst>
        </op>
        <op>
                <action>COPY</action>
                <src>copy_src</src>
                <dst>copy_dst</dst>
        </op>
        <op>
                <action>RESTORE</action>
                <src>/tmp/restor.conf.orig</src>
                <dst>/etc/restor.conf</dst>
        </op>
</object>

I have learned how to read/parse this file via xmlReadFile with 
XML_PARSE_NOBLANKS | XML_PARSE_DTDVALID. I have also manged to walk the 
tree and print node name and content.
Here is what I want to be able to do:
An operation specific C function (such as processManifestCopy()) would 
read/parse this file and then
proceed to identify all the objects (or files) whose op.action is COPY 
and then do the appropriate thing.

My question is:
Is there any libxml function that can do some of this work for me. The 
only way that I know how to
do this at this point is to walk the tree with xmlDocGetRootElement(), 
and xmlNode and
put each object in a struct, then examine the op.action, if this is what 
I want, then move on to the next
object, else reuse this struct, such that at the end of the day, I have 
a link list of structures.
I'm sure libXML is more powerfull than ... here is a xmlDocPtr, now take 
it from here....

Hmm, maybe you are asking for XPath.
In case you don't know XPath: http://www.w3.org/TR/xpath (or search for
a tutorial).

With "xmllint" you can play with XPath through the "shell" feature:

--------
$ cat manifest.xml
<?xml version="1.0"?>
<object>
  <op>
    <action>COPY</action>
    <src>MY-SOURCE</src>
    <dst>MY-DESTINATION</dst>
  </op>
</object>
--------
$ xmllint --shell manifest.xml
/ > xpath /object/op/action[text() = "COPY"]
Object is a Node Set :
Set contains 1 nodes:
1  ELEMENT action
--------

This selected a list of <action> elements which have a text-node
with a content of "COPY".

--------
/ > xpath /object/op/action[text() =
"COPY"]/following-sibling::src/text()
Object is a Node Set :
Set contains 1 nodes:
1  TEXT
    content=MY-SOURCE
--------

This selected a list of text-nodes which are contained in the
<src> elements, which in turn are "following" siblings of the
previously selected list of <action> elements.
So this way, you get directly to the "MY-SOURCE" content.

There should be plenty of ways to process the semantics of
your XML document with the help of XPath.

As a suggestion, perhaps xmlsoft.org site's author could provide some 
SEE ALSO links
in the description area of functions. Currently one learns about a 
function such as xmlReadFile()
but what do you do with that, where do you go from here....not 
there...basically the old unix man page recommendations.

I'm not the author of it, but at
http://www.xmlsoft.org/examples/index.html you'll find some
XPath examples to get you started.

Additionally you could validate your XML document with,
let's say... Libxml2's XML Schema processor ;-)
(Don't get confused by me saying this; I just want to
promote the use of the XML Schema processor to help finding
bugs)

Regards,

Kasimier



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