Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Getting the XML node containing a search text...

by biswanath_c (Beadle)
on Oct 26, 2009 at 22:14 UTC ( #803346=perlquestion: print w/ replies, xml ) Need Help??
biswanath_c has asked for the wisdom of the Perl Monks concerning the following question:


Hi

I am trying to write a perl script that'd give me the list of nodes in an XML doc that contain a search string. What's the best way to achieve this goal? I'd prefer getting the XPaths of the nodes that contain the search string.


Comment on Getting the XML node containing a search text...
Re: Getting the XML node containing a search text...
by toolic (Chancellor) on Oct 26, 2009 at 22:43 UTC
    give me the list of nodes in an XML doc that contain a search string
    Can you be more specific regarding what you want to search? Are you trying to match only element names? Element text? Attribute names? Attribute values? All of the above?

    You should post a tiny sample of your XML and the search string.

    XML::Twig has xpath capabilities.


      given a string, I would want to search for all nodes in a an XML document that 'contain' the search string 'in' its value (not exact match). So, i want to search for nodes whose 'value' contain a given search string and would like to get the xpath of all such matching nodes.
Re: Getting the XML node containing a search text...
by ramrod (Hermit) on Oct 27, 2009 at 14:47 UTC
    Using XML::LibXML this works for me
    use strict; use XML::LibXML; #Parse XML my $template = 'xmlsample.xml'; my $parser = XML::LibXML->new(); my $doc = $parser->parse_file($template); #Search all text nodes for my $node ($doc->findnodes( '//text()' )) #Print xpaths if criteria matched {my $search = $node->nodeValue; if ($search =~ m/InsertSearchCriteriaHere/) {print $node->nodePath();} }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2014-08-01 05:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (256 votes), past polls