Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Using XPath in libXML

by ramrod (Hermit)
on Feb 09, 2009 at 15:25 UTC ( #742458=perlquestion: print w/ replies, xml ) Need Help??
ramrod has asked for the wisdom of the Perl Monks concerning the following question:

I am rather new to the world of Perl, and I was wondering if anyone had some advice/experience using XPath and libXML.

I am trying to comb through an XML style document and replace some of the data. So far I have only been partially successful, and I have also generated some questions that I have not been able to answer:

How can I access an attribute of the root node?
"Regular" XPath uses . and @ characters which have different uses in Perl, is there some workaround for using these in libXML?
I found a script using "/text()", which is a valid means to access the value of an element. Are there other similar/related calls I can use?

Pretty much, it looks like I am missing the basic syntax for XPath in libXML. Any help with this is appreciated.

Thanks.

Comment on Using XPath in libXML
Re: Using XPath in libXML
by Corion (Pope) on Feb 09, 2009 at 15:29 UTC

    You don't show us your code, you don't show us your data, and you don't show us the error message you get. My crytal ball claims that maybe you're just running double quoted strings where you should be using single quoted strings. See perlop about how these differ - one interpolates and the other doesn't.

    For the record, I had no problems using the XPath implementation of XML::LibXML. Accessing an attribute of a root node (or rather, getting the node containing it) would be done using

    /root@foo

    as the XPath expression. A likely sample could be:

    ... my $query = '/root@foo'; my @nodes = $document->find_nodes($query);
      Ah, the quotes mix up. I didn't consider that. The criteria I was using to access some of the nodes was in the form:

      my $query = "//property[color = 'Blue']/OtherProperty/text()";

      This threw me off. I switched to using the single (literal) quotes and this has helped quite a bit. However I am still curious if there are other "/text()" style path calls. (Just in case there are better ways of doing things.)

      Thanks for you help.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://742458]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (13)
As of 2014-11-24 17:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (144 votes), past polls