Tài liệu XML by Example- P4 - Pdf 87

Paths
The syntax for XML paths is similar to file paths. XML paths start from
the root of the document and list elements along the way. Elements are
separated by the “
/
” character.
The root of the document is “
/
”. The root is a node that sits before the top-
level element. It represents the document as a whole.
The following four paths match respectively the title of the article
(
<title>XML Style Sheets</title>
), the keywords of the article, the top-
most article element, and all sections in the article. Note that the last path
matches several elements in the source tree.
/article/title
/article/keywords
/article
/article/section
TIP
Note that “
/
” points to the immediate children of a node. Therefore,
/article/title
selects the main title of the article (XML Style Sheets), not all the titles below the arti-
cle element. It won’t select the section titles.
To select all the descendants from a node, use the “
//
” sequence.
/article//title

It is possible to combine paths in a match with the “
|
” character, such as
title | p
matches title or
p
elements.
135
Basic XSLT
EXAMPLE
07 2429 CH05 2.29.2000 2:21 PM Page 135
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Matching on Attributes
Paths can match on attributes, too. The following template applies only to
“mailto” URLs:
<xsl:template match=”url[@protocol=’mailto’]”>
<A>
<xsl:attribute name=”HREF”>mailto:<xsl:apply-templates/>
</xsl:attribute>
<xsl:apply-templates/>
</A>
</xsl:template>
<A href=”mailto:”>
➥</A>
It matches
<url protocol=”mailto”></url>
that has a protocol attribute with the value “mailto” but it does not match
<url> />. The more generic
url
path would

OUTPUT
07 2429 CH05 2.29.2000 2:21 PM Page 136
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
last()
returns the position of the last node in the current node set
count()
returns the number of nodes in the current node set
not()
negates the argument
contains()
returns true if the first argument contains the second argument
starts-with()
returns true if the first argument starts with the second argu-
ment
New functions are declared in JavaScript, Java, C++, and so on, with the
xsl:functions
element.
<xsl:template match=”/”>
<xsl:value-of select=”psol:today()”/>
</xsl:template>
<xsl:functions ns=”psol” type=”text/javascript”>
function today() {
return Date().toString()
}
</xsl:functions>
CAUTION
Be aware that this element was still very much in flux in the draft we used to prepare
this chapter.
Deeper in the Tree
After loading the style sheet, the XSL processor loads the source document.

<BODY>
<xsl:apply-templates/>
</BODY>
</HTML>
</xsl:template>
Because the root sits before the top-level element, it is ideal to create the
top-level element of the resulting tree. For HTML, it is the
HTML
element
with
HEAD
and
BODY
elements.
When it encounters
xsl:appy-templates
, the processor moves to the first
child of the current node. The first child of the root is the top-level element
or the article element.
The style sheet defines no templates for the article but can match template
against a built-in template. Built-in templates are not defined in the style
sheet. They are predefined by the processor.
138
Chapter 5: XSL Transformation
07 2429 CH05 2.29.2000 2:21 PM Page 138
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
<xsl:template match=”* | /”>
<xsl:apply-templates/>
</xsl:template>
NOTE

copyright, abstract, keywords, and section. Copyright, abstract, and key-
words match the same rule as abstract and generate no output in the
resulting tree.
139
Basic XSLT
07 2429 CH05 2.29.2000 2:21 PM Page 139
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
The section element, however, matches the default template and the proces-
sor moves to its children, title, and
p
elements. The processor continues to
match rules with nodes until it has exhausted all the nodes in the original
document.
Creating Nodes in the Resulting Tree
Sometimes it is useful to compute the value or the name of new nodes. The
following template creates an HTML anchor element that points to the
URL. The anchor has two attributes. The first one,
TARGET
, is specified
directly in the template. However, the processor computes the second
attribute,
HREF
, when it applies the rule.
<xsl:template match=”url”>
<A TARGET=”_blank”>
<xsl:attribute name=”HREF”>
<xsl:apply-templates/>
</xsl:attribute>
<xsl:apply-templates/>
</A>

EXAMPLE
OUTPUT
07 2429 CH05 2.29.2000 2:21 PM Page 140
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
P
RIORITY
There are rules to prioritize templates. Without going into too many details,
templates with more specific paths take precedence over less specific tem-
plates. In the following example, the first template has a higher priority
than the second template because it matches an element with a specific
attribute.
<xsl:template match=”url[@protocol=’mailto’]”>
<A>
<xsl:attribute name=”HREF”>mailto:<xsl:apply-templates/>
</xsl:attribute>
<xsl:apply-templates/>
</A>
</xsl:template>
<xsl:template match=”url”>
<A TARGET=”_blank”>
<xsl:attribute name=”HREF”>
<xsl:apply-templates/>
</xsl:attribute>
<xsl:apply-templates/>
</A>
</xsl:template>
If there is a conflict between two templates of equivalent priority, then the
XSL processor can either report an error or choose the template that
appears last in the style sheet.
Supporting a Different Medium

<xsl:template match=”url”>
<xsl:text>[</xsl:text>
<xsl:apply-templates/>
<xsl:text>]</xsl:text>
</xsl:template>
<xsl:template match=”p”>
<xsl:text>
</xsl:text>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match=”abstract | date | keywords | copyright”/>
</xsl:stylesheet>
Logically enough, the
xsl:stylesheet
element does not declare a name-
space for the resulting tree. This style sheet also makes heavy use of text
nodes.
142
Chapter 5: XSL Transformation
07 2429 CH05 2.29.2000 2:21 PM Page 142
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
<xsl:template match=”section/title”>
<xsl:text>*** </xsl:text>
<xsl:apply-templates/>
<xsl:text> ***</xsl:text>
</xsl:template>
The following command line creates the text in Listing 5.5.
java –classpath
➥c:\lotusxsl\xerces.jar;c:\lotusxs\lotusxsl.jar
➥com.lotus.xsl.Process

Customized Views
Currently, most people access the Web through a browser on a Windows
PC. Some people use Macintoshes, others use UNIX workstations. This will
change in the future as more people turn to specialized devices. Already
WebTV has achieved some success with a browser in a TV set.
Mobile phones and PDAs, such as the popular PalmPilot, will be increas-
ingly used for Web browsing. Ever tried surfing on a PalmPilot? It works
surprisingly well but, on the small screen, many Web sites are not readable
enough.
One solution to address the specific needs of smaller devices might be to
use XHTML, an XML simplified version of HTML. XHTML is based on
HTML but it has an XML syntax (as opposed to an SGML syntax). It is also
designed to be modular as it is expected smaller devices will implement
only a subset of the recommendation.
According to the W3C, these new platforms might account for up to 75% of
Web viewing by the year 2002. What can you do about it? Will you have to
maintain several versions of your Web site: one for existing Web browsers
and one for each new device with its own subset?
XSL to the rescue! It will be easy to manage the diversity of browsers and
platforms by maintaining the document source in XML and by converting
to the appropriate XHTML subset with XSLT. In essence, this is how I
manage the e-zine. Figure 5.7 illustrates how this works.
144
Chapter 5: XSL Transformation
Figure 5.7: Maintain one XML document and convert it to the appropriate
markup language.
07 2429 CH05 2.29.2000 2:21 PM Page 144
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Where to Apply the Style Sheet
So far, we have converted the XML documents before publishing them. The

EXAMPLE
continues
07 2429 CH05 2.29.2000 2:21 PM Page 145
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Listing 5.6: continued
<article fname=”19990101_xsl”>
<title>XML Style Sheets</title>
<date>January 1999</date>
<copyright>1999, Benoît Marchal</copyright>
<abstract>Style sheets add flexibility to document viewing.</abstract>
<keywords>XML, XSL, style sheet, publishing, web</keywords>
<section>
<p>Send comments and suggestions to <url protocol=”mailto”>bmarchal@
➥pineapplesoft.com</url>.</p>
</section>
<section>
<title>Styling</title>
<p>Style sheets are inherited from SGML, an XML ancestor. Style sheets
➥originated in publishing and document management applications. XSL is XML’s
➥standard style sheet, see <url> /></section>
<section>
<title>How XSL Works</title>
<p>An XSL style sheet is a set of rules where each rule specifies how to format
➥certain elements in the document. To continue the example from the previous
➥section, the style sheets have rules for title, paragraphs and keywords.</p>
<p>With XSL, these rules are powerful enough not only to format the document
➥but also to reorganize it, e.g. by moving the title to the front page or
➥extracting the list of keywords. This can lead to exciting applications of XSL
➥outside the realm of traditional publishing. For example, XSL can be used to
➥convert documents between the company-specific markup and a standard one.</p>

<HTML>
<HEAD>
<TITLE>Pineapplesoft Link</TITLE>
</HEAD>
<BODY>
<xsl:apply-templates/>
</BODY>
</HTML>
</xsl:template>
<xsl:template match=”section/title”>
<P><I><xsl:apply-templates/></I></P>
</xsl:template>
<xsl:template match=”article/title”>
<P><B><xsl:apply-templates/></B></P>
</xsl:template>
<xsl:template match=”url”>
<A TARGET=”_blank”>
<xsl:attribute name=”href”>
<xsl:apply-templates/>
</xsl:attribute>
<xsl:apply-templates/>
</A>
147
Where to Apply the Style Sheet
continues
07 2429 CH05 2.29.2000 2:21 PM Page 147
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Listing 5.7: continued
</xsl:template>
<xsl:template match=”url[@protocol=’mailto’]”>

CAUTION
Internet Explorer 5.0 does not use the standard priority rules. Therefore, the default
templates must be at the top of the style sheet; otherwise, they would have higher
priority than our rules.
Advanced XSLT
XSLT is a powerful transformation mechanism. So far, we have only used a
subset of it. Our resulting document follows a structure that is close to the
original document. Elements might have been added or removed from the
tree but they are not reorganized.
Yet, it is often useful to reorganize completely the source document. For
example, we might want to create a table of contents at the beginning of
the document.
This is possible with the
xsl:value-of
element.
xsl:value-of
inserts arbi-
trary elements from the source tree anywhere in the resulting tree.
Listing 5.8 is a more sophisticated style sheet that, among other things,
creates a table of contents.
Listing 5.8: A More Powerful XSLT Style Sheet
<?xml version=”1.0” encoding=”ISO-8859-1”?>
<!DOCTYPE xsl:stylesheet [
<!ENTITY copy “&#0169;”>
]>
<xsl:stylesheet
xmlns:xsl=” />149
Advanced XSLT
EXAMPLE
continues

</HTML>
</xsl:template>
<xsl:template name=”title”>
<xsl:value-of select=”/article/title”/> (
<xsl:value-of select=”/article/date”/> )
</xsl:template>
150
Chapter 5: XSL Transformation
07 2429 CH05 2.29.2000 2:21 PM Page 150
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
<xsl:template match=”section/title”>
<P><I><A>
<xsl:attribute name=”NAME”>
<xsl:value-of select=”generate-id()”/>
</xsl:attribute>
<xsl:apply-templates/>
</A></I></P>
</xsl:template>
<xsl:template match=”url”>
<A TARGET=”_blank”>
<xsl:attribute name=”href”>
<xsl:value-of select=”.”/>
</xsl:attribute>
<xsl:value-of select=”.”/>
</A>
</xsl:template>
<xsl:template match=”url[@protocol=’mailto’]”>
<A>
<xsl:attribute name=”href”>mailto:<xsl:value-of select=”.”/>
</xsl:attribute>

<P>Send comments and suggestions to <A
href=”mailto:”></A>.</P>
<P><I><A name=”N-614609527”>Styling</A></I></P>
<P>Style sheets are inherited from SGML, an XML ancestor. Style sheets
➥originated in publishing and document management applications. XSL is XML’s
➥standard style sheet, see <A target=”_blank”
➥href=” name=
➥”N-634270327”>How XSL Works</A></I></P>
<P>An XSL style sheet is a set of rules where each rule specifies how to format
➥certain elements in the document. To continue the example from the previous
➥section, the style sheets have rules for title, paragraphs and keywords.</P>
<P>With XSL, these rules are powerful enough not only to format the document
➥but also to reorganize it, e.g. by moving the title to the front page or
➥extracting the list of keywords. This can lead to exciting applications of XSL
➥outside the realm of traditional publishing. For example, XSL can be used to
➥convert documents between the company-specific markup and a standard one.</P>
<P><I><A name=”N-653406839”>The Added Flexibility of Style Sheets</A></I></P>
<P>Style sheets are separated from documents. Therefore one document can have
➥more than one style sheet and, conversely, one style sheet can be shared
➥amongst several documents.</P>
<P>This means that a document can be rendered differently depending on the media
➥or the audience. For example, a “managerial” style sheet may present a summary
➥view of a document that highlights key elements but a “clerical” style sheet
➥may display more detailed information.</P>
<P>Copyright ©1999, Benoît Marchal</P></BODY></HTML>
152
Chapter 5: XSL Transformation
OUTPUT
07 2429 CH05 2.29.2000 2:21 PM Page 152
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.

07 2429 CH05 2.29.2000 2:21 PM Page 153
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Calling a Template
When the same styling instructions are used at different places, group
them in a named template. For example, titles appear in the HTML title
and in the body of the document.
<xsl:template name=”title”>
<xsl:value-of select=”/article/title”/> (
<xsl:value-of select=”/article/date”/> )
</xsl:template>
<!-- ... -->
<P><B><xsl:call-template name=”title”/></B></P>
This simplifies maintenance because changes to the title are localized in a
single template.
xsl:include
imports a style sheet into the current style sheet. The follow-
ing imports the
core.xsl
style sheet:
<xsl:include href=”core.xsl”/>
xsl:include
must be a direct child of
xsl:stylesheet
, it cannot appear in
xsl:template
for example.
Repetitions
Sometimes a path points to several elements. For example,
article/
section/title


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status