XSL Transform学习笔记1

XML   2008-07-04 15:47   阅读0   评论0  
字号:    
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="application/xml" href="xs.xsl"?>
<?xml-stylesheet type="text/xsl" href="xs.xsl"?>
<PERIODIC_TABLE>
<ATOM STATE="GAS">
<NAME>Hydrogen</NAME>
<SYMBOL>H</SYMBOL>
<ATOMIC_NUMBER>1</ATOMIC_NUMBER>
<ATOMIC_WEIGHT>1.00794</ATOMIC_WEIGHT>
<BOILING_POINT UNITS="Kelvin">20.28</BOILING_POINT>
<MELTING_POINT UNITS="Kelvin">13.81</MELTING_POINT>
<DENSITY UNITS="grams/cubic centimeter">
<!-- At 300K, 1 atm -->
0.0000899
</DENSITY>
</ATOM>
<ATOM STATE="GAS">
<NAME>Helium</NAME>
<SYMBOL>He</SYMBOL>
<ATOMIC_NUMBER>2</ATOMIC_NUMBER>
<ATOMIC_WEIGHT>4.0026</ATOMIC_WEIGHT>
<BOILING_POINT UNITS="Kelvin">4.216</BOILING_POINT>
<MELTING_POINT UNITS="Kelvin">0.95</MELTING_POINT>
<DENSITY UNITS="grams/cubic centimeter"><!-- At 300K -->
0.0001785
</DENSITY>
</ATOM>
</PERIODIC_TABLE>




<xsl:template match=/>
Each xsl:template element has a match attribute that specifies which nodes of the
input document the template is instantiated for.The content of the xsl:template element is the template to be instantiated. A template
may contain both text, which will appear literally in the output document, and
XSLT instructions that copy data from the input XML document to the result.


<xsl:apply-templates/>
To get beyond the root, you have to tell the formatting engine to process the children
of the root. In general, to include content in the child nodes, you have to
recursively process the nodes through the XML document.An xsl:apply-templates element tells the processor
to compare each child node of the matched source element against the templates
in the style sheet and, if a match is found, output the template for the
matched node.


<xsl:template match=ATOM>
<xsl:apply-templates select=NAME/>
</xsl:template>
To choose a particular set of children instead of all children,
supply xsl:apply-templates with a select attribute designating the
children to be selected,
If no select attribute is present, all
child element, text, comment, and processing instruction nodes are selected.
(Attribute and namespace nodes are not selected.)


<xsl:template match=ATOM>
<xsl:value-of select=NAME/>
</xsl:template>
Computing the Value of a Node
Node Type Value
Root The value of the root element
Element The concatenation of all parsed character data contained in the
element, including character data in any of the descendants of the
element
Text The text of the node; essentially the node itself
Attribute The normalized attribute value as specified by Section 3.3.3 of the
XML 1.0 Recommendation; basically the attribute value after
entities are resolved and leading and trailing white space is
stripped; does not include the name of the attribute, the equals
sign, or the quotation marks
Namespace The URI of the namespace
Processing instruction The data in the processing instruction; does not include the target,
<? or ?>
Comment The text of the comment, <!-- and --> not included
If there are multiple possible items that could be
selected, only the first one will be chosen.


Processing Multiple Elements
1
<xsl:template match=PERIODIC_TABLE>
<xsl:apply-templates select=ATOM/>
</xsl:template>
<xsl:template match=ATOM>
<xsl:value-of select=./>
</xsl:template>
The select=. in the second template tells the formatter to take the value of the
matched node, ATOM in this example.
2
<xsl:template match=PERIODIC_TABLE>
<xsl:for-each select=ATOM>
<xsl:value-of select=./>
</xsl:for-each>
</xsl:template>
The xsl:for-each element processes each
element chosen by its select attribute in turn. However, no additional template
rule is required. Instead, the content of the xsl:for-each element serves as a template.
This is useful when you need to format the same content differently in different
places in the style sheet.


"/" To specify the root node in a rule, you give its match attribute the value /,as in the following
example:
<xsl:template match=/>
<DOCUMENT>
<xsl:apply-templates/>
</DOCUMENT>
</xsl:template>
This rule applies to the root node and only the root node of the input tree.


Matching element names
the most basic pattern contains a single element name
that matches all elements with that name. For example, this template matches ATOM
elements and makes their ATOMIC_NUMBER children bold:
<xsl:template match=ATOM>
<B><xsl:value-of select=ATOMIC_NUMBER/></B>
</xsl:template>


Wildcards
Sometimes you want a single template to apply to more than one element. You can
indicate that a template matches all elements by using the asterisk wildcard (*) in
place of an element name in the match attribute.
<xsl:template match=*>
<P>
<xsl:value-of select=./>
</P>
</xsl:template>
In the event that
two rules both match a single node, by default the more specific one takes precedence.
In this case, that means that ATOM elements will use the template with
match=ATOM instead of a template that merely has match=*.

You can place a namespace prefix in front of the asterisk to indicate that only elements
in a particular namespace should be matched.
<xsl:template match=svg:*>
<DIV>
<xsl:value-of select=./>
</DIV>
</xsl:template>



Matching children with /
You can
use the / symbol to match hierarchies of elements. Alone, the / symbol refers to
the root node. However, between two names it indicates that the second is the child
of the first.
For example, this template rule marks SYMBOL elements that are children
of ATOM elements strong. It does nothing to SYMBOL elements that are not
direct children of ATOM elements.
<xsl:template match=ATOM/SYMBOL>
<STRONG><xsl:value-of select=./></STRONG>
</xsl:template>

Finally, as previously described, a / by itself selects the root node of the document.
For example, this rule applies to all PERIODIC_TABLE elements that are root elements
of the document:
<xsl:template match=/PERIODIC_TABLE>
<HTML><xsl:apply-templates/></HTML>
</xsl:template>



Matching descendants with //
The
double slash, //, refers to a descendant at an arbitrary level. For example, this template
rule applies to all NAME descendants of PERIODIC_TABLE, no matter how
deep:
<xsl:template match=PERIODIC_TABLE//NAME>
<EM><xsl:value-of select=./></EM>
</xsl:template>

The // operator at the beginning of a pattern selects any descendant of the root
node. For example, this template rule processes all ATOMIC_NUMBER elements while
completely ignoring their location:
<xsl:template match=//ATOMIC_NUMBER>
<EM><xsl:value-of select=./></EM>
</xsl:template>


Matching by ID
This is done with the id() selector, which contains
the ID value in single quotes. For example, this rule makes the element with
the ID e47 bold:
<xsl:template match=id(e47)>
<B><xsl:value-of select=./></B>
</xsl:template>
This assumes, of course, that the elements you want to select in this fashion have
an attribute declared as type ID in the source documents DTD.
ID-type attributes are not simply attributes with the name ID.


Matching attributes with @
the @ sign matches against attributes and selects nodes
according to attribute names.
For example, this template rule matches UNITS attributes,
and wraps them in an I element:
<xsl:template match=@UNITS>
<I><xsl:value-of select=./></I>
</xsl:template>
However, merely adding this rule to the style sheet will not automatically produce
italicized units in the output,because attributes are not children of the elements
that contain them. Therefore, by default, when an XSLT processor is walking the
tree, it does not see attribute nodes. You have to explicitly process them using
xsl:apply-templates with an appropriate select attribute.

For example, the pattern BOILING_POINT/@UNITS refers to the UNITS attribute of a
BOILING_POINT element.
You can also use the @* wildcard to match all attributes of an element, for example
BOILING_POINT/@* to match all attributes of BOILING_POINT elements. You can
also add a namespace prefix after the @ to match all attributes in a declared
namespace. For example, @xlink:* matches all the XLink attributes



Matching comments with comment()
To match a comment, use the comment() pattern. Although this pattern has functionlike
parentheses, it never actually takes any arguments. For example, this template
rule italicizes all comments:
This rule only matches comments that occur inside DENSITY elements:
<xsl:template match=DENSITY/comment()>
<I><xsl:value-of select=./></I>
</xsl:template>
The following rule matches all comments, and copies them back out
again using the xsl:comment element.
<xsl:template match=comment()>
<xsl:comment><xsl:value-of select=./></xsl:comment>
</xsl:template>



Matching processing instructions
with processing-instruction()
The processing-instruction() function matches processing instructions. The
argument to processing-instruction() is a quoted string giving the target of the
processing instruction to select. If you do not include an argument, all processing
instructions are matched.
This rule only matches xml-stylesheet processing instructions:
<xsl:template
match=processing-instruction(xml-stylesheet)>
<xsl:comment>
<xsl:value-of select=./>
</xsl:comment>
</xsl:template>
The value of a processing instruction is simply everything
between the white space following its name and the closing ?>.




Matching text nodes with text()
Text nodes are generally ignored as nodes, although their values are included as
part of the value of a selected element. However, the text() operator does enable
you to specifically select the text child of an element. Despite the parentheses, this
operator takes no arguments.
The main reason this operator exists is for the default rules. XSLT processors must
provide the following default rule whether the author specifies it or not:
<xsl:template match=text()>
<xsl:value-of select=./>
</xsl:template>
This means that whenever a template is applied to a text node, the text of the node
is output. If you do not want the default behavior, you can override it. For example,
including the following empty template rule in your style sheet will prevent text
nodes from being output unless specifically matched by another rule:
<xsl:template match=text() />



Using the or operator |
The vertical bar (|) allows a template rule to match multiple patterns. If a node
matches one pattern or the other, it will activate the template. For example, this
template rule matches both ATOMIC_NUMBER and ATOMIC_WEIGHT elements:
<xsl:template match=ATOMIC_NUMBER|ATOMIC_WEIGHT>
<B><xsl:apply-templates/></B>
</xsl:template>




Testing with [ ]
you can test
for more details about the nodes that match a pattern using []. You can perform
many different tests, including the following:
? Whether an element contains a given child, attribute, or other node
? Whether the value of an attribute is a certain string
? Whether the value of an element contains a string
? What position a given node occupies in the hierarchy
<!-- Include nothing for arbitrary atoms -->
<xsl:template match=ATOM />
<!-- Include a table row for atoms that do have
melting points. This rule will override the
previous one for those atoms that do have
melting points. -->
<xsl:template match=ATOM[MELTING_POINT]>
<TR>
<TD><xsl:value-of select=NAME/></TD>
<TD><xsl:value-of select=MELTING_POINT/></TD>
</TR>
</xsl:template>
The test brackets can contain more than simply a child-element name. In fact, they
can contain any XPath expression.
<xsl:template match=”ATOM[NAME | SYMBOL]”>
</xsl:template>
This template rule matches ATOM elements with a DENSITY child element that has a
UNITS attribute:
<xsl:template match=”ATOM[DENSITY/@UNITS]”>
</xsl:template>
To find all child elements that have UNITS attributes, use * to find all elements and
[@UNITS] to winnow those down to the ones with UNITS attributes, like this:
<xsl:template match=”ATOM”>
<xsl:apply-templates select=”*[@UNITS]”/>
</xsl:template>
One type of pattern testing that proves especially useful is string equality. An
equals sign (=) can test whether the value of a node identically matches a string.
For example, this template finds the ATOM element that contains an ATOMIC_NUMBER
element whose content is the string 10 (Neon).
<xsl:template match=”ATOM[ATOMIC_NUMBER=’10’]”>
This is Neon!
</xsl:template>

评论(?)
阅读(?)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
网易公司版权所有 ©1997-2009