📄 ch08_03.htm
字号:
<html><head><title>XSLT (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 & 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 & 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="ch08_02.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="ch08_04.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div>
<h2 class="sect1">8.3. XSLT</h2>
<p>If you<a name="INDEX-708" />
think<a name="INDEX-709" /> of XPath as a
regular expression syntax, then <a name="INDEX-710" />XSLT is its pattern
substitution mechanism. XSLT is an XML-based programming language for
describing how to transform one document type into another. You can
do some amazing things with XSLT, such as describe how to turn any
XML document into HTML or tabulate the sum of figures in an
XML-formatted table. In fact, you might not need to write a line of
code in Perl or any language. All you really need is an XSLT script
and one of the dozens of transformation engines available for
processing XSLT.
</p>
<a name="perlxml-CHP-8-SIDEBAR-1" /><blockquote><table border="1" cellpadding="6"><tr><td>
<h4 class="objtitle">The Origin of XSLT</h4>
<p>XSLT stands for XML Style Language: Transformations. The name means
that it's a component of the XML Style Language
(XSL), assigned to handle the task of converting input XML into a
special format called XSL-FO (the FO stands for
"Formatting Objects"). XSL-FO
contains both content and instructions for how to make it pretty when
displayed.
</p>
<p>Although it's stuck with the XSL name, XSLT is more
than just a step in formatting; it's an important
XML processing tool that makes it easy to convert from one kind of
XML to another, or from XML to text. For this reason, the W3C (yup,
they created XSLT too) released the recommendation for it years
before the rest of XSL was ready.
</p>
<p>To read the specification and find links to XSLT tutorials, look at
its home page at <a href="http://www.w3.org/TR/xslt">http://www.w3.org/TR/xslt</a>.
</p>
</td></tr></table><p></blockquote>
<p>An XSLT transformation script is itself an XML document. It consists
mostly of rules called <em class="emphasis">templates</em>, each of
which tells how to treat a specific type of node. A template usually
does two things: it describes what to output and defines how
processing should continue.
</p>
<p>Consider the script in <a href="ch08_03.htm#perlxml-CHP-8-EX-9">Example 8-9</a>. </p>
<a name="perlxml-CHP-8-EX-9" /><div class="example">
<h4 class="objtitle">Example 8-9. An XSLT stylesheet </h4>
<blockquote><pre class="code"><xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="html">
<xsl:text>Title: </xsl:text>
<xsl:value-of select="head/title"/>
<xsl:apply-templates select="body"/>
</xsl:template>
<xsl:template match="body">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="h1 | h2 | h3 | h4">
<xsl:text>Head: </xsl:text>
<xsl:value-of select="."/>
</xsl:template>
<xsl:template match="p | blockquote | li">
<xsl:text>Content: </xsl:text>
<xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet></pre></blockquote>
</div>
<p>This transformation script converts an <a name="INDEX-711" />HTML
document into ASCII with some extra text labels. Each
<tt class="literal"><xsl:template></tt> element is a rule that
matches a part of an XML document. Its content consists of
instructions to the XSLT processor describing what to output.
Directives like <tt class="literal"><xsl:apply-templates></tt> direct
processing to other elements (usually descendants). We
won't go into detail about XSLT syntax, as whole
books on the subject are available. Our intent here is to show how
you can combine XSLT with Perl to do powerful XML munching.
</p>
<p>You might wonder, "Why do I need to use another
language to transform XML when I can do that with the Perl I already
know?" True, XSLT doesn't do
anything you couldn't do in Perlish coding. Its
value comes in the ease of learning the language. You can learn XSLT
in few hours, but to do the same things in Perl would take much
longer. In our experience writing software for XML, we found it
convenient to use XSLT as a configuration file that nonprogrammers
could maintain themselves. Thus, instead of viewing XSLT as
competition for Perl, think of it more as a complementary technology
that you can access through Perl when you need to.
</p>
<p>How do Perl hackers employ the power of XSLT in their programs? <a href="ch08_03.htm#perlxml-CHP-8-EX-10">Example 8-10</a> shows how to perform an XSLT transformation on
a document using <tt class="literal">XML::LibXSLT</tt>, Matt
Sergeant's interface to the super-fast GNOME library
called LibXSLT, one of several XSLT solutions available from your
CPAN toolbox.<a href="#FOOTNOTE-29">[29]</a>
</p><blockquote class="footnote"> <a name="FOOTNOTE-29" /><p>[29]Others that are currently available
include the pure-Perl <tt class="literal">XML::XSLT</tt> module, and
<tt class="literal">XML::Sablotron</tt>, based on the Expat and Sablotron C
libraries (the latter of which is an XSLT library by the Ginger
Alliance: <a href="http://www.gingerall.com">http://www.gingerall.com</a>).</p> </blockquote>
<a name="perlxml-CHP-8-EX-10" /><div class="example">
<h4 class="objtitle">Example 8-10. A program to run an XSLT transformation </h4>
<blockquote><pre class="code">use XML::LibXSLT;
use XML::LibXML;
# the arguments for this command are stylesheet and source files
my( $style_file, @source_files ) = @ARGV;
# initialize the parser and XSLT processor
my $parser = XML::LibXML->new( );
my $xslt = XML::LibXSLT->new( );
my $stylesheet = $xslt->parse_stylesheet_file( $style_file );
# for each source file: parse, transform, print out result
foreach my $file ( @source_files ) {
my $source_doc = $parser->parse_file( $source_file );
my $result = $stylesheet->transform( $source_doc );
print $stylesheet->output_string( $result );
}</pre></blockquote>
</div>
<p>The nice thing about this program is that it parses the stylesheet
only once, keeping it in memory for reuse with other source
documents. Afterwards, you have the document tree to do further work,
if necessary:
</p>
<ul><li>
<p>Postprocess or preprocess the text of the document with
search-replace routines.
</p>
</li><li>
<p>Pluck a piece of the document out to transform just that bit. </p>
</li><li>
<p>Run an iterator over the tree to handle some nodes that would be too
difficult to process in XSLT.
</p>
</li></ul>
<p>The possibilities are endless and, as always in Perl, whatever you
want to do<a name="INDEX-712" />, there's more
than one way<a name="INDEX-713" /> to do it.
</p>
<hr width="684" align="left" />
<div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch08_02.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="ch08_04.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">8.2. XPath</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">8.4. Optimized Tree Processing</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 © 2002</a> O'Reilly & 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 + -