XSL Transform学习笔记2 2008-07-07 14:26

字号:    
XPath Expressions for Selecting Nodes
The select attribute is used in xsl:apply-templates, xsl:value-of, xsl:foreach,
xsl:copy-of, xsl:variable, xsl:param, and xsl:sort to specify exactly
which nodes are operated on. The value of this attribute is an expression written in
the XPath language. The XPath language provides a means of identifying a particular
element, group of elements, text fragment, or other part of an XML document.
The XPath syntax is used both for XSLT and XPointer.



Expression Axes
Axis Selects From
ancestor The parent of the context node, the parent of the parent of the
context node, the parent of the parent of the parent of the
context node, and so forth back to the root node
ancestor-or-self The ancestors of the context node and the context node itself
attribute The attributes of the context node
child The immediate children of the context node
descendant The children of the context node, the children of the children
of the context node, and so forth
descendant-or-self The context node itself and its descendants
following All nodes that start after the end of the context node,
excluding attribute and namespace nodes
following-sibling All nodes that start after the end of the context node and have
the same parent as the context node
namespace The namespace of the context node
parent The unique parent node of the context node
preceding All nodes that finish before the beginning of the context node,
excluding attribute and namespace nodes
preceding-sibling All nodes that start before the beginning of the context node
and have the same parent as the context node
self The context node

The axis is generally followed by a
double colon (::) and a node test that further winnows down this node-set.
<xsl:template match=”ATOM”>
<TR>
<TD>
<xsl:value-of select=”child::NAME”/>
</TD>
<TD>
<xsl:value-of select=”child::ATOMIC_NUMBER”/>
</TD>
<TD>
<xsl:value-of select=”child::ATOMIC_WEIGHT”/>
</TD>
</TR>
</xsl:template>
For example, this rule
matches all elements. If the matched element is a PERIODIC_TABLE, that very
PERIODIC_TABLE is selected in xsl:value-of.
<xsl:template match=”*”>
<xsl:value-of select=”ancestor-or-self::PERIODIC_TABLE”/>
</xsl:template>





Node tests
Instead of the name of a node, the axis can be followed by one of these four nodetype
functions:
? comment()
? text()
? processing-instruction()
? node()
The comment() function selects a comment node. The text() function selects a text
node. The processing-instruction() function selects a processing instruction
node, and the node() function selects any type of node. (The * wildcard only selects
element nodes.) The processing-instruction() node type can also contain an
optional argument specifying the name of the processing instruction to select.





Hierarchy operators
You can use the / and // operators to string expressions together.select=”parent::*/child::NAME”




Abbreviated syntax
Abbreviated Syntax for XPath Expressions
Abbreviation Full
. self::node()
.. parent::node()
name child::name
@name attribute::name
// /descendant-or-self::node()/





Expression types
there are four types of expressions in XPath:
? Node-sets
? Booleans
? Numbers
? Strings
In addition, XSLT adds one type to this list, the result tree fragment. This is what an
xsl:template element creates. However, it is not used by other non-XSLT uses of
XPath.

Node-sets
A node-set is an unordered group of nodes from the input document.

Functions That Operate on or Return Node-sets
Function Return Type Returns
position() number The position of the context node in the
context node list; the first node in the
list has position 1.
last() number The number of nodes in the context
node list; this is the same as the
position of the last node in the list.
count(node-set) number The number of nodes in node-set.
id(string1 node-set A node-set containing all the elements
string2 string3...) anywhere in the same document that
have an ID named in the argument list;
the empty set if no element has the
specified ID.
key(string name, node-set A node-set containing all nodes in this
Object value) document that have a key with the
specified value. Keys are set with the
top-level xsl:key element.
document(string URI, node-set A node-set from the document referred
string base) to by the URI; the exact subset of nodes
are chosen from that document are
selected by the XPointer in the URI’s
fragment identifier. If the URI does not
have a fragment identifier, then the root
element of the named document is the
node-set. Relative URIs are relative to
the base URI given in the second
argument. If the second argument is
omitted, then relative URIs are relative
to the URI of the style sheet (not the
source document!).
Function Return Type Returns
local-name(node-set) string The local name (everything after the
namespace prefix) of the first node in
the node-set argument; can be used
without any arguments to get the local
name of the context node.
namespace-uri string The URI of the namespace of the first
(node-set) node in node-set; can be used
without any arguments to get the URI of
the namespace of the context node;
returns an empty string if the node is
not in a namespace.
name(node-set) string The qualified name (both prefix and
local part) of the first node in nodeset;
can be used without an argument
to get the qualified name of the context
node.
generate-id(node-set) string A unique string for the first node in the
argument node-set; can be used
without any argument to generate an ID
for the context node.


If an argument of the wrong type is passed to one of these functions, XSLT will
attempt to convert that argument to the correct type; for example, by converting
the number 12 to the string “12”. However, no arguments can be converted to
node-sets.


Booleans
A boolean has one of two values: true or false. XSLT allows any kind of data to be
transformed into a boolean. This is often done implicitly when a string or a number
or a node-set is used where a boolean is expected

converts an argument of any type to a boolean according to these rules:
? A number is false if it’s zero or NaN (a special symbol meaning Not a Number,
used for the result of dividing by zero and similar illegal operations); true
otherwise.
? An empty node-set is false. All other node-sets are true.
? An empty result tree fragment is false. All other result tree fragments are true.
? A zero length string is false. All other strings are true.

Booleans are also produced as the result of expressions involving these operators:
? = Equal to
? != Not equal to
? < Less than (really &lt;)
? > Greater than
? <= Less than or equal to (really &lt;=)
? >= Greater than or equal to
The < sign must be replaced by &lt; even when used as the less-than operator
in an XML document such as an XSLT style sheet.
The keywords and and or logically combine two boolean expressions according to
the normal rules of logic.
The not() function reverses the result of an operation.
<xsl:template
match=”ATOMIC_NUMBER[position()=1 and position()=last()]”>
<xsl:value-of select=”.”/>
</xsl:template>

<xsl:template match=”ATOM[not(position()=1)]”>
<xsl:value-of select=”.”/>
</xsl:template>

There are three remaining functions that return booleans:
? true() always returns true.
? false() always returns false.
? lang(code) returns true if the current node has the same language (as given
by the xml:lang attribute) as the code argument.




Numbers
XPath numbers are 64-bit IEEE 754 floating-point doubles. Even numbers like 43 or
–7000 that look like integers are stored as doubles.

? Booleans are 1 if true, 0 if false.
? A string is trimmed of leading and trailing white space, then converted to a
number in the fashion you would expect; for example, the string “12” is converted
to the number 12. If the string cannot be interpreted as a number, it is
converted to NaN.
? A node-set is converted to a string; the string is then converted to a number.


XPath provides the standard four arithmetic operators:
? + for addition
? - for subtraction
? * for multiplication
? div for division (the more common / is already used for other purposes in
XPath)
XPath also provides the less familiar mod operator, which takes the remainder of
two numbers.
? floor() returns the greatest integer less than or equal to the number.
? ceiling() returns the smallest integer greater than or equal to the number.
? round() rounds the number to the nearest integer.
? sum() returns the sum of its arguments.




Strings
A string is a sequence of Unicode characters. Other data types can be converted to
strings using the string() function according to these rules:
? Node-sets are converted to strings by taking the value of the first node in the
set, as calculated by the xsl:value-of element, according to the rules given
in Table 15-1.
? Result tree fragments are converted by acting as if they’re contained in a single
element, and then taking the value of that imaginary element. Again, the
value of this element is calculated by the xsl:value-of element according to
the rules given in Table 15-1. That is, all the result tree fragment’s text (but not
markup) is concatenated.
? A number is converted to a European-style number string such as –12 or
3.1415292.
? Boolean false is converted to the English word false. Boolean true is converted
to the English word true.

XPath String Functions
Function Return Type Returns
starts-with Boolean True if main_string starts with
(main_string, prefix_string; false otherwise.
prefix_string)
contains Boolean True if the contained_string is
(containing_string, part of the containing_string;
contained_string) false otherwise.
substring(string, String length characters from the
offset, length) specified offset in string; or all
characters from the offset to the
end of the string if length is
omitted; length and offset are
rounded to the nearest integer if
necessary; the first character in the
string is at offset 1.
substring-before String The part of the string from the first
(string, character up to (but not including)
marker-string) the first occurrence of markerstring.
substring-after String The part of the string from the
(string, point immediately after the first
marker-string) occurrence of marker-string to
the end of string.
string-length(string) Number The number of characters in
string.
normalize-space String The string after leading and
(string) trailing white space is stripped and
runs of white space are replaced
with a single space; if the argument
is omitted the string value of the
context node is normalized.
translate(string, String Returns string with occurrences of
replaced_text, characters in replaced_text
replacement_text) replaced by the corresponding
characters from
replacement_text.
concat(string1, String Returns the concatenation of as
string2, . . . ) many strings as are passed as
arguments in the order they were
passed.

Function Return Type Returns
format-number String Returns the string form of number
(number, formatted according to the specified
format-string, format-string as if by Java 1.1’s
locale-string) java.text.DecimalFormat class
(see http://java.sun.com/
products/archive/jdk/1.1/in
dex.html); the locale-string is
an optional argument that provides
the name of the xsl:decimalformat
element used to interpret
the format-string.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
网易公司版权所有 ©1997-2009