Re: [xml] Newbie question on libxml


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


<?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)>


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: (or search for
a tutorial).

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

$ cat manifest.xml
<?xml version="1.0"?>
$ 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() =
Object is a Node Set :
Set contains 1 nodes:

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



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