[redirects]
[story]
[news_id]200810243[/news_id]
[new_url]new_url_one[/new_url]
[/story]
[story]
[news_id]200809024[/news_id]
[new_url]new_url_two[/new_url]
[/story]
[/redirects]tested in python, but hopefully works for any proper xpath implementation./redirects/story[news_id/text()="200810243"]/new_url/text()
#!/usr/bin/perl -wuse strict;use XML::XPath;my $xp = XML::XPath->new( xml => <<EOXML<redirects> <story> <news_id>200810243</news_id> <new_url>new_url_one</new_url> </story> <story> <news_id>200809024</news_id> <new_url>new_url_two</new_url> </story></redirects>EOXML);my $nodeset = $xp->find( "redirects/story[news_id=${ARGV[0]}]/new_url" );foreach my $node ($nodeset->get_nodelist) { print "FOUND\n\n", XML::XPath::XMLParser::as_string( $node ), "\n\n";}#!/usr/bin/env pythonimport libxml2import sysdoc = libxml2.parseDoc( """<redirects> <story> <news_id>200810243</news_id> <new_url>new_url_one</new_url> </story> <story> <news_id>200809024</news_id> <new_url>new_url_two</new_url> </story></redirects>""" );for node in doc.xpathEval( "redirects/story[news_id=%s]/new_url" % sys.argv[ 1 ] ): node.saveTo( sys.stdout, format=True );
You are not logged in, either login or create an account to post comments
preg_match( '#(?<=<news_id>' . $news_id . '</news_id>\s*<new_url>)[^<>]*(?=</news_id>)#', $str, $matches );
yes, yes, and now you have two problems
posted by rjt at 4:35 PM on December 8, 2008