XML and PHP [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

XML and PHP [Electronic resources] - نسخه متنی

Vikram Vaswani

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید















XSLT



As you know, XML, by itself, merely provides the constructs necessary to describe data; it offers no information about how that data is to be presented to a user. Hence, a requirement arises for a separate stylesheet language to handle the presentation aspects of an XML application. This stylesheet language is the Extensible Stylesheet Language (XSL), and it consists of the following three components:




XML Path Language (XPath), which provides constructs to locate specific nodes or node collections in an XML document tree




XSL Transformations (XSLT), which is responsible for restructuring, or transforming, XML data into a different format




XSL Formatting Objects (XSL-FO), which is responsible for the formatting and presentation of the result.





The very first Working Draft of XSL appeared on the W3C's web site in August 1998. At that time, the specification combined XSLT, XPath, and XSL-FO into a single document; the three were later separated into independent entities. The XSLT and XPath specifications were approved by the W3C as W3C Recommendations in November 1999, with the XSL 1.0 specification achieving Recommendation status in October 2001. The W3C's XSL Working Group is currently working to develop the next versions of these specifications in order to address new XML technologies and develop new extensions to the language.


By separating the layout and presentation of data from the data itself, the XSL family of technologies makes it possible to create different representations and views of the same data, simply by applying a different stylesheet to the same XML source. For example, a single inventory statement that is marked up in XML could be combined with different stylesheets to create views customized to the requirements of a purchase manager (who needs to view stock quantities), an accountant (who needs to view costs), and a customer (who needs to view unit availability). In a similar vein, a separate presentation layer also makes it easier to export data into a variety of different formatsthe same XML source could therefore be used to generate ASCII documents, HTML web pages, and WML content.


This chapter focuses primarily on XSLT, which provides high-level constructs to transform a source XML document (referred to in the official specification as a source tree) into a new document (the result tree). It does this by using a stylesheet containing template rules to locate and match structures in the source tree, and transform them into new structures in the corresponding result tree.


Perhaps the best way to understand this is with a simple example. Consider the XML document in Listing 4.1, which contains a (fictional) list of best-selling books.


Listing 4.1 Simple XML Book List (list.xml)



<?xml version="1.0"?>
<list>
<item>
<title>Waking Up With The Red-Eyed Bed Monster</title>
<author>J. I. M. Somniac</author>
<blurb>The blood-chillingly true story of one man's fight against the
monsters under his bed.</blurb>
</item>
<item>
<title>The Case Of The Hungry Hippopotamus</title>
<author>P. I. Hardhat</author>
<blurb>A tough private eye is hired to
solve the most challenging case of
his career.</blurb>
</item>
<item>
<title>Making Money, Losing Friends</title>
<author>T. Backstabber</author>
<blurb>The bestselling self-help book
for corporate executives on the fast
track.</blurb>
</item>
<item>
<title>The Idiot's Guide to Sandwiches</title>
<author>B. L. Tuhmatto</author>
<blurb>Making tasty sandwiches has never been so easy!</blurb>
</item>
</list>


Listing 4.2 is its associated XSLT stylesheet.


Listing 4.2 XSLT Stylesheet (list.xsl)



<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:
xsl="http://www.w3.org/1999/XSL/Transform">
<!-- set up the main page container -->
<xsl:template match="/">
<html>
<head>
</head>
<body>
<xsl:apply-templates />
</body>
</html>
</xsl:template>
<!-- look for the list node -->
<xsl:template match="/list" xml:space="preserve">
<h3><font face="Arial">Bestsellers</font></h3>
<!-- iterate through the item nodes under the list node -->
<ul>
<xsl:for-each select="item">
<li>
<font face="Arial"><b><xsl:value-of select="title" /></b> - <xsl:value-of
select="author" /></font>
<br />
<font face="Arial"><i><xsl:value-of select="blurb" /></i></font>
<p />
</li>
</xsl:for-each>
</ul>
</xsl:template>
</xsl:stylesheet>


When I put the two together and run them through an XSLT processor, Figure 4.1 is what I get.



Figure 4.1. An HTML page created via an XSL Transformation.






The rules in the stylesheet are matched against the XML markup to create new HTML structures; the resulting HTML output is viewable in any web browser. If, for example, I decided to output WML rather than HTML, I would need merely to modify the presentation layer (the stylesheet) to reflect the new formatting required in the result tree with no modifications required to the original XML data source. (Take a look at Listing 4.20 for an example of how this works.)


This is a very simple exampleXSLT allows for a much greater level of complexity, both in creating template rules and in manipulating the result tree. I won't get into the details herethis chapter assumes a familiarity with the basics of XSLTalthough you can find links to a number of interesting articles and tutorials (for both novices and experts) on this book's companion web site.




/ 84