⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch03_06.htm

📁 Perl & XML. by Erik T. Ray and Jason McIntosh ISBN 0-596-00205-X First Edition, published April
💻 HTM
字号:
<html><head><title>XML::XPath (Perl and XML)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Erik T. Ray and Jason McIntosh" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly &amp; Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="059600205XL" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Perl and XML" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Perl &amp; XML" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch03_05.htm"><img alt="Previous" border="0" src="../gifs/txtpreva.gif" /></a></td><td align="center" valign="top" width="228" /><td align="right" valign="top" width="228"><a href="ch03_07.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">3.6. XML::XPath</h2><p>We've<a name="INDEX-259" /> <a name="INDEX-260" /> seenexamples of parsers that dutifully deliver the entire document toyou. Often, though, you don't need the whole thing.When you query a database, you're usually lookingfor only a single record. When you crack open a telephone book,you're not going to sit down and read the wholething. There is obviously a need for some mechanism of extracting aspecific piece of information from a vast document. Look no furtherthan XPath.</p><p>XPath is a recommendation from the folks who brought youXML.<a href="#FOOTNOTE-18">[18]</a> It's a grammar for writing expressionsthat pinpoint specific pieces of documents. Think of it as anaddressing scheme. Although we'll save thenitty-gritty of XPath wrangling for <a href="ch08_01.htm">Chapter 8, "Beyond Trees: XPath, XSLT, and More"</a>, wecan tantalize you by revealing that it works much like a mix ofregular expressions with Unix-style file paths. Not surprisingly,this makes it an attractive feature to add to parsers.</p><blockquote class="footnote"> <a name="FOOTNOTE-18" /><p>[18]Check out the specification at <a href="http://www.w3.org/TR/xpath/">http://www.w3.org/TR/xpath/</a>.</p></blockquote><p>Matt Sergeant's <tt class="literal">XML::XPath</tt> moduleis a solid implementation, built on the foundation of<tt class="literal">XML::Parser</tt>. Given an XPath expression, it returnsa list of all document parts that match the description.It's an incredibly simple way to perform somepowerful search and retrieval work.</p><p>For instance, suppose we have an address book encoded in XML in thisbasic form:</p><blockquote><pre class="code">&lt;contacts&gt;  &lt;entry&gt;    &lt;name&gt;Bob Snob&lt;/name&gt;    &lt;street&gt;123 Platypus Lane&lt;/street&gt;    &lt;city&gt;Burgopolis&lt;/city&gt;    &lt;state&gt;FL&lt;/state&gt;    &lt;zip&gt;12345&lt;/zip&gt;  &lt;/entry&gt; &lt;!--More entries go here--&gt;&lt;/contacts&gt;</pre></blockquote><p>Suppose you want to extract all the zip codes from the file andcompile them into a list. <a href="ch03_06.htm#perlxml-CHP-3-EX-7">Example 3-7</a> shows how youcould do it with XPath.</p><a name="perlxml-CHP-3-EX-7" /><div class="example"><h4 class="objtitle">Example 3-7. Zip code extractor </h4><blockquote><pre class="code">use XML::XPath;my $file = 'customers.xml';my $xp = XML::XPath-&gt;new(filename=&gt;$file);# An XML::XPath nodeset is an object which contains the result of# smacking an XML document with an XPath expression; we'll do just# this, and then query the nodeset to see what we get.my $nodeset = $xp-&gt;find('//zip');my @zipcodes;                   # Where we'll put our resultsif (my @nodelist = $nodeset-&gt;get_nodelist) {  # We found some zip elements! Each node is an object of the class  # XML::XPath::Node::Element, so I'll use that class's 'string_value'  # method to extract its pertinent text, and throw the result for all  # the nodes into our array.  @zipcodes = map($_-&gt;string_value, @nodelist);  # Now sort and prepare for output  @zipcodes = sort(@zipcodes);  local $" = "\n";  print "I found these zipcodes:\n@zipcodes\n";} else {  print "The file $file didn't have any 'zip' elements in it!\n";}</pre></blockquote></div><p>Run the program on a document with three entries andwe'll get something like this:</p><blockquote><pre class="code">I found these zipcodes:036421233382649</pre></blockquote><p>This module also shows an example of tree-based parsing, by the way,as its parser loads the whole document into an object tree of its owndesign and then allows the user to selectively interact with parts ofit via XPath expressions. This example is just a sample of what youcan do with advanced tree processing modules. You'llsee more of these modules in <a href="ch08_01.htm">Chapter 8, "Beyond Trees: XPath, XSLT, and More"</a>.</p><p><tt class="literal">XML::LibXML</tt>'s element objectssupport a <tt class="literal">findnodes( )</tt> method that works much like<tt class="literal">XML::XPath</tt>'s, using the invoking<tt class="literal">Element</tt> object as the current context andreturning a list of objects that match the query.We'll play<a name="INDEX-261" /> <a name="INDEX-262" /> with this functionality later in<a href="ch10_01.htm">Chapter 10, "Coding Strategies"</a>.</p><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch03_05.htm"><img alt="Previous" border="0" src="../gifs/txtpreva.gif" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img alt="Home" border="0" src="../gifs/txthome.gif" /></a></td><td align="right" valign="top" width="228"><a href="ch03_07.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">3.5. XML::LibXML</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img alt="Book Index" border="0" src="../gifs/index.gif" /></a></td><td align="right" valign="top" width="228">3.7. Document Validation</td></tr></table></div><hr width="684" align="left" /><img alt="Library Navigation Links" border="0" src="../gifs/navbar.gif" usemap="#library-map" /><p><p><font size="-1"><a href="copyrght.htm">Copyright &copy; 2002</a> O'Reilly &amp; Associates. All rights reserved.</font></p><map name="library-map"><area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -