Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

XML to XPath: Get xpath from xml

by AjayPerl (Initiate)
on Jun 14, 2013 at 03:27 UTC ( [id://1038880]=perlquestion: print w/replies, xml ) Need Help??

AjayPerl has asked for the wisdom of the Perl Monks concerning the following question:

Is there a way in PERL to parse an XML file and creates XPath expressions for each element encountered.

My goal is to achieve following from this ex xml file :

<root> <elemA>one</elemA> <elemA attribute1='first' attribute2='second'>two</elemA> <elemB>three</elemB> <elemC> <elemB>five</elemB> </elemC> </root> to produce the following output : //root[1]/elemA[1]='one' //root[1]/elemA[2]='two' //root[1]/elemA[2][@attribute1='first'] //root[1]/elemA[2][@attribute2='second'] //root[1]/elemB[1]='three' //root[1]/elemC[1]/elemB[1]='five'

Replies are listed 'Best First'.
Re: XML to XPath: Get xpath from xml
by space_monk (Chaplain) on Jun 14, 2013 at 04:15 UTC

    Use XML::Twig and the get_xpath method looks a possible method of achieving what you're looking to do.

    If you spot any bugs in my solutions, it's because I've deliberately left them in as an exercise for the reader! :-)
Re: XML to XPath: Get xpath from xml
by Anonymous Monk on Jun 14, 2013 at 07:16 UTC

      Thanks..let me try this out

Re: XML to XPath: Get xpath from xml
by nysus (Parson) on Apr 05, 2019 at 05:40 UTC

    A big thanks from 6 years into the future for asking this question. Been hunting all over for a solution to this.

    $PM = "Perl Monk's";
    $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest Vicar";
    $nysus = $PM . ' ' . $MCF;
    Click here if you love Perl Monks

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-19 03:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found