没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论












275
This is the Title of the Book, eMatter Edition
Copyright © 2000 O’Reilly & Associates, Inc. All rights reserved.
Chapter 7
In this chapter:
• XSLT Processing
Mechanics
• Single-Template
Stylesheets
• Understanding Input
and Output Options
• Improving Flexibility
with Multiple
Templates
7
Transforming XML
with XSLT
We’ve used XSLT stylesheets in previous chapters to transform database-driven
XML into HTML pages, XML datagrams of a particular vocabulary, SQL scripts,
emails, and so on. If you’re a developer trying to harness your database informa-
tion to maximum advantage on the Web, you’ll find that XSLT is the Swiss Army
knife you want permanently attached to your belt. In a world where the exchange
of structured information is core to your success, and where the ability to rapidly
evolve and repurpose information is paramount, Oracle XML developers who fully
understand how to exploit XSLT are way ahead of the pack.
XSLT 1.0 is the W3C standard language for describing transformations between
XML documents. It is closely aligned with the companion XPath 1.0 standard and
works in concert with it. As we’ll see in this chapter, XPath let’s you say what to
transform, and XSLT provides the complementary language describing how to
carry out the transformation. An XSLT stylesheet describes a set of rules for trans-
forming a source XML document into a result XML document. An XSLT processor
is the software that carries out the transformation based on these rules.
In the simple examples in previous chapters, we have seen three primary ways to
use the Oracle XSLT processor. We’ve used the oraxsl command-line utility, the
XSLT processor’s programmatic API, and the <?xml-stylesheet?> instruction to
associate a stylesheet with an XSQL page. In this chapter, we begin exploring the full
power of the XSLT language to understand how best to use it in our applications.
XSLT Processing Mechanics
An XSLT stylesheet describes a transformation that operates on the tree-structured
infoset of a source XML document and produces a tree of nodes as its output.

276 Chapter 7: Transforming XML with XSLT
This is the Title of the Book, eMatter Edition
Copyright © 2000 O’Reilly & Associates, Inc. All rights reserved.
Consider a simple XML document like this:
<!-- Emp.xml -->
<ROWSET>
<ROW num="1">
<EMPNO>7839</EMPNO>
<ENAME>KING</ENAME>
</ROW>
<ROW num="2">
<EMPNO>7788</EMPNO>
<ENAME>SCOTT</ENAME>
</ROW>
</ROWSET>
A transformation of this document operates on the document’s corresponding
node tree (shown in Figure 7-1). The tree of nodes for an XML document always
starts with a root node that represents the document itself. Child nodes of the root
can be the single document element node—<ROWSET>, in our example—as well as
comments and processing instructions. Child nodes of the document element can
be any combination of text nodes and element nodes, each of which may, in turn,
have similar child nodes. This nesting of nodes forms a tree.
Remember that an XML document can look like this:
<ROWSET>
<ROW num="1">
<X>Y</X>
</ROW>
</ROWSET>
or it can look like this:
<ROWSET><ROW num="1"><X>Y</X></ROW></ROWSET>
While both expressions contain a logically equivalent element struc-
ture, the former example contains additional whitespace (denoted by
WS nodes in Figure 7-1) to give it that indented look. Specifically, it
contains a carriage return at the end of every line followed by a
series of spaces at the start of the next line. When considering an
XML document as a tree of nodes, don’t forget that the text nodes
containing whitespace also count as nodes the same as text like 7788
or SCOTT. Since you can’t see it, whitespace is easy to forget about.
To carry out a transformation, an XSLT processor requires two ingredients:
• The source tree of nodes
• An XSLT stylesheet containing a set of transformation rules
An XSLT stylesheet is an XML document that uses elements from the XSLT vocabu-
lary to describe a transformation. The document element of every stylesheet is an

XSLT Processing Mechanics 277
This is the Title of the Book, eMatter Edition
Copyright © 2000 O’Reilly & Associates, Inc. All rights reserved.
<xsl:stylesheet> element whose content is a set of rules describing the transfor-
mation to be performed. Each rule in the stylesheet contains an associated XPath
pattern that matches the nodes in the source document to which that rule should
apply. Each rule is called a template and is represented by an <xsl:template>
element with a match="pattern" attribute for its associated XPath match pattern.
For example, a rule like this:
<xsl:template match="/">
<!-- Some Result Content: Elements, Attributes, Text, etc. -->
</xsl:template>
applies to the root node of the document, matching the XPath pattern “/”.
Figure 7-1. Node tree for a simple ROWSET document
/
<!-- Source -->
<ROWSET>
WS
<ROW> num="1"
WS
<EMPNO>
7839
WS
<ENAME>
KING
WS
<ROW> num="2"
WS
<EMPNO>
7788
WS
<ENAME>
SCOTT

278 Chapter 7: Transforming XML with XSLT
This is the Title of the Book, eMatter Edition
Copyright © 2000 O’Reilly & Associates, Inc. All rights reserved.
Similarly, a rule like this:
<xsl:template match="ROWSET/ROW[ENAME]">
<!-- Some Result Content: Elements, Attributes, Text, etc. -->
</xsl:template>
applies only to <ROW> elements in the source document that have an <ENAME>
child element and occur as immediate children of a <ROWSET> element.
Each rule is called a template because the literal elements and attributes contained
inside the body of the rule act as a blueprint for constructing a part of the result
tree. The XSLT processor constructs the content of a rule’s template in the result
tree whenever it processes a source node matching the rule’s pattern. Figure 7-2
illustrates what happens when a rule like this:
<xsl:template match="ROWSET/ROW[ENAME]">
<Employee id="NX-{EMPNO}">
<xsl:value-of select="ENAME"/>
</Employee>
</xsl:template>
is triggered by processing a <ROW> element in the source tree that matches the
XPath pattern ROWSET/ROW[ENAME].
Figure 7-2. Matching source tree node and constructing result fragment
<ROWSET>
<ROW>
WS
<EMPNO>
7839
WS
<ENAME>
KING
Source Tree Fragment
Matches
<xsl:template match="ROWSET/ROW[ENAME]">
<Employee id="NX-{EMPNO}">
<xsl:value-of select="ENAME"/>
</Employee>
</xsl:template>
<xsl:stylesheet>
Constructs
<Employee>
KING
id="NX-7839"
Result Tree Fragment

XSLT Processing Mechanics 279
This is the Title of the Book, eMatter Edition
Copyright © 2000 O’Reilly & Associates, Inc. All rights reserved.
As the matching template is instantiated, the following three things occur:
1. Literal result elements and attributes in the template are created in the result
tree. Result elements and attributes that are not from the XSLT namespace are
considered “literal” since they are constructed as is in the result tree. In the
example just given, the <Employee> element and its id attribute are created.
2. Any attribute value templates of the form {XPathExpr} contained within lit-
eral attribute values are replaced by the value of their XPath expression. In the
example, the {EMPNO} inside the literal attribute value NX-{EMPNO} is
replaced by the value of the EMPNO XPath expression. This evaluates to 7839,
so the final value for the id attribute in the result tree is NX-7839.
3. Any elements in the XSLT namespace are processed in document order. The
<xsl:value-of> element is processed and is replaced by a text node con-
taining the string value of the XPath expression in its select attribute—in this
case, KING.
The basic operation can be summarized as follows: when a node in the source
matches a rule’s pattern, the content of that rule is created in the result tree. Once
you grasp this basic operation, the overall XSLT processing model is easy to
understand. Given a source tree and a stylesheet, the XSLT processor carries out
the transformation described by rules in the stylesheet by following a sequence of
steps, just like the ones we have described.
A list of nodes in the source tree is processed to create a portion, or “fragment,” of
the result tree. The result tree fragment for the list of nodes is created by process-
ing the nodes in order and concatenating each of their respective result tree frag-
ments together in the same order. The node in the current node list being
processed is known, not surprisingly, as the current node. The current node is
processed by considering the set of all possible rules that match it and then select-
ing the single rule that matches it best. Only a single rule is ever used to process
the current node in the current node list.
To start the process, the XSLT processor begins with a node list containing only the
document root. It finds the template matching this root node—typically the rule
with match="/"—and instantiates the contents of the template in the result tree,
following the three basic processing steps to complete the job. If the template con-
tains elements from the XSLT namespace that select other nodes to process, the
sequence of matching and template content instantiation continues recursively until
there are no nodes left to process. When processing is completed, the result tree
represents the target document produced by the transformation.
剩余34页未读,继续阅读
资源评论


pwl2014
- 粉丝: 1
- 资源: 12
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


安全验证
文档复制为VIP权益,开通VIP直接复制
