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