List Info

Thread: How to extract attribute value in node using XPath




How to extract attribute value in node using XPath
user name
2007-10-03 19:15:46
Below is example code snippet which extracts NAME node value. I want to extract MESSAGE node Type attribute value.

XML FILE:

<MESSAGE Type=";test">
 &nbsp; &nbsp; <NAME&gt;extract attribute value<NAME>
</MESSAGE>

CODE SNIPPET:

foreach my $node ( $xmlFH->;find(";MESSAGE&quot;)->get_nodelist ) {
 &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; 
 &nbsp; &nbsp;  my $name ; &nbsp; = $node->find("NAME")->string_value;

}


Basically, I need to extract attribute value in a node using XPath. How should I do that?

Thanks
Sunil.



RE: How to extract attribute value in node using XPath
country flaguser name
Australia
2007-10-03 20:08:46

Hello,

 

You can use XML:XPath Package to use XPath in your application. Once you install XPath, you need to “use” that and following code will help you using XPath

 

my $xp = XML::XPath->new(filename => $ConfigFilePath);

my $nodeset = $xp->find(“Your XPath Comes here”);&nbsp;   ; &nbsp; &nbsp; &nbsp;

return $xp->getNodeText($nodeset);

 

Hope this help.

 

Regards,

Sumit Gupta

http://www.vikasumit.com

 

From: perl-xml-bounceslistserv.ActiveState.com [mailto:perl-xml-bounceslistserv.ActiveState.com] On Behalf Of Sunil
Sent: Thursday, October 04, 2007 5:46 AM
To: perl-xmllistserv.activestate.com
Subject: How to extract attribute value in node using XPath

 

Below is example code snippet which extracts NAME node value. I want to extract MESSAGE node Type attribute value.

XML FILE:

<MESSAGE Type=";test">
&nbsp; &nbsp;  <NAME&gt;extract attribute value<NAME>
&lt;/MESSAGE>

CODE SNIPPET:

foreach my $node ( $xmlFH->;find(";MESSAGE&quot;)->get_nodelist ) {
   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; &nbsp;   ;
 ; &nbsp; &nbsp; my $name ; &nbsp; = $node-&gt;find(&quot;NAME&quot;)->string_value;

}


Basically, I need to extract attribute value in a node using XPath. How should I do that?

Thanks
Sunil.



No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.5.488 / Virus Database: 269.13.39/1045 - Release Date: 10/2/2007 6:43 PM

Re: How to extract attribute value in node using XPath
user name
2007-10-04 03:06:35
On Wed, 2007-10-03 at 17:15 -0700, Sunil wrote:
> Below is example code snippet which extracts NAME node
value. I want
> to extract MESSAGE node Type attribute value. 
> 
> XML FILE:
> 
> <MESSAGE Type="test">
>      <NAME>extract attribute value<NAME>
> </MESSAGE>
> 
> CODE SNIPPET:
> 
> foreach my $node (
$xmlFH->find("MESSAGE")->get_nodelist ) {
>                                                        
                                                            
        
>       my $name    =
$node->find("NAME")->string_value;
> 
> }
> 
> 
> Basically, I need to extract attribute value in a node
using XPath.
> How should I do that?

Most simply:

        use XML::XPath;
        
        my $xp = XML::XPath->new(filename =>
'xp1.xml');
        
        print $xp->findvalue('//MESSAGE/Type');


I've just used findvalue() there for convenience, you could
use find()
equally well but with the extra steps to get the value.

I'd recommend reading e.g. http://www.w3.org/TR/xpath
 and possibly such
as http://www.w3schools.
com/xpath/ 

/J
_______________________________________________
Perl-XML mailing list
Perl-XMLlistserv.ActiveState.com
To unsubscribe: http:/
/listserv.ActiveState.com/mailman/mysubs

Re: How to extract attribute value in node using XPath
user name
2007-10-04 05:00:21
On Wed, 3 Oct 2007, Sunil wrote:

> Below is example code snippet which extracts NAME node
value. I want to
> extract MESSAGE node Type attribute value.
>
> XML FILE:
>
> <MESSAGE Type="test">
>     <NAME>extract attribute value<NAME>
> </MESSAGE>
>
> CODE SNIPPET:
>
> foreach my $node (
$xmlFH->find("MESSAGE")->get_nodelist ) {
>
>
>      my $name    =
$node->find("NAME")->string_value;
>
> }
>
>
> Basically, I need to extract attribute value in a node
using XPath. How
> should I do that?

 	I'd be looking at something like

 	$xmlFH->findvalue('//MESSAGE/Type')

 	However, I recommend that you delve into XPath enough to
be able to 
resolve these kinds of problems.  Here are some tips:

1.	Use Data:umper to
print out the results of anything.  Often you'll
 	just get an object reference, but it'll tell you what type
of object
 	you're dealing with (ie. XML::LibXML::Attr vs.
XML::LibXML::Element).
2.	If you print the results of the serialize function, you
can see what's
 	in your node.  So, try something like this:


------------------------------------------------------------
-------------------
#!/usr/bin/perl

use	Data:umper;
use	Scalar::Util;

.......


$node = $xmlFH->find('whatever');
shownode($node);

sub	shownode {
 	my($node) = _;

 	print(('-' x 40) . "n");
 	print Dumper $node;
 	if(blessed($node) and $node->can('serialize')) {
 		print $node->serialize();
 	} else {
 		print "Can't serialize this!n";
 	}
 	print(('-' x 40) . "n");
}

------------------------------------------------------------
-------------------

 	That should point you towards any difficulties you
encounter.

 	


------------------------------------------------------------
---------
| Name: Tim Nelson                 | Because the Creator is,
       |
| E-mail: waylandsmartchat.net.au | I am                   
       |
------------------------------------------------------------
---------

----BEGIN GEEK CODE BLOCK----
Version 3.12
GCS d+++ s+: a- C++$ U+++$ P+++$ L+++ E- W+ N+ w--- V- 
PE(+) Y+>++ PGP->+++ R(+) !tv b++ DI++++ D G+
e++>++++ h! y-
-----END GEEK CODE BLOCK-----
_______________________________________________
Perl-XML mailing list
Perl-XMLlistserv.ActiveState.com
To unsubscribe: http:/
/listserv.ActiveState.com/mailman/mysubs

[1-4]

about | contact  Other archives ( Real Estate discussion Medical topics )