Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Donate Today!

Do you enjoy these
technical forums?
Donate Today! Click Here

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

zitvig (TechnicalUser)
21 Jul 06 6:28
Hi all,

I'm working with some XML files using perl-5.8.7 and p5-XML-XPath-1.13 on FreeBSD6.

How do I get the name of an XML element using XPath? I know the position of where this element would be. In the example below I need a function that returns the string "mystery_tag", knowing that this would be the first child of /root/data/details/

CODE

<root>
    <info>some text</info>
    <more_info></more_info>
    <data>
        <details>
            <mystery_tag>
                <a>some</a>
                <b>text</b>
                <c>here</c>
            </mystery_tag>
            <stuff></stuff>
        </details>
    </data>
</root>

My knowledge of perl is quite limited so any links with examples using XPath would be appreciated.

thanks
PaulTEG (TechnicalUser)
21 Jul 06 17:12
Have you considered any of the excellent XML parsers over at http://search.cpan.org.

Sorry dunno the 1st thing about XPath, but always go to cpan when I'm looking for something like that

HTH
--Paul

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments winky smile

rharsh (TechnicalUser)
23 Jul 06 15:25
I'm with Paul, I don't know much about XPath - I did give it a shot with some results that could be helpful.  Did you look at the docs for XPath?  This is pretty much straight out of the documentation:

CODE

use XML::XPath;
use XML::XPath::XMLParser;

my $xp = XML::XPath->new(filename => 'test.txt');
my $nodeset = $xp->find('/root/data/details'); # find all nodes under details

foreach my $node ($nodeset->get_nodelist) {
    print "FOUND\n\n",
        XML::XPath::XMLParser::as_string($node),
        "\n\n";
}
Which prints:

CODE

FOUND

<details>
            <mystery_tag>
                <a>some</a>
                <b>text</b>
                <c>here</c>
            </mystery_tag>
            <stuff />
        </details>
So you could split the results of XML::XPath::XMLParser::as_string($node) into an array and look at the second element in that array to find the name of your mystery tag.

What are you doing with the XML info once it's parsed?  Is there a particular reason you're using XML::XPath as opposed to another parser?

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close