Run XSLT
XSLT Transform Tool is a well-designed webpage where you can directly try out your XSLT code, for several reasons:
- It's handy, i.e., online XSLT interpreter
- If you create local
.xml
and.xsl
files, you will have cross-domain problems under Chrome (not IE). - It uses local storage to save your data, so closing the browser tab doesn't lose your code
- It executes your code whenever you make changes to your code
- It separates XML files and XSLT code into two tabs
Note: Due to the different browser support for XSLT, you will encounter compatibility problems. My experience is that it works best under Firefox. Remind that XSLT 2.0 is not currently supported on all browsers.
Helloworld
XSL file itself is a legal XML document. XSL tags are defined under the namespace http://www.w3.org/1999/XSL/Transform
, the top wrapper element is <xsl:stylesheet>
. The following code prints Hello world!
regardless of XML input.
<?xml version="1.0" encoding="UTF-8" ?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <xsl:text>Hello world!</xsl:text> </xsl:template> </xsl:stylesheet>
XSLT Directive Tags
- xsl:apply-template
Applying a template is just like calling a function. A template is the definition of a function.
<xsl:apply-template name="customTemplate" />
This directive assigns an attribute to the wrapping tag.
Attribute | Required | Description |
---|---|---|
name | Yes | specifies attribute name |
namespace | No | URI |
<TagFoo> <xsl:attribute name="AttrFoo"> AttrVal </xsl:attribute> </TagFoo>
It includes external .xsl
files with a lower precedence. It must reside directly under <xsl:stylesheet>
or <xsl:transform>
element.
Attribute | Required | Description | Position |
---|---|---|---|
href | Yes | URI | Top level |
It writes texts to output. If escaping is disabled, angle brackets will be rendered as it is.
Attribute | Required | Value | Default |
---|---|---|---|
disable-output-escaping | No | yes | no | no |
XSLT Placeholder
XSLT Expressions
- expression
- node-set-expression
- boolean-expression
- number-expression
- string-expression
- pattern
XSLT Patterns
We may better call them selectors, because we use them as the value of match
or select
attribute.
- *
- prefix:*
- @*
- text()
- node()
- element_name
- child::element_name
- element_name1 | element_name2 | @id
- element_name/text()
- parent//text()
- element[@id='foo']
- prefix:parent[@id='foo']/element_name
select all the nodes
select all the nodes from the namespace prefix
select all nodes who have attribute
select any text node
select all nodes except the root
select the node with specified tag name
select any public node (doesn't belongs to any namespace)
selector operator or
select all text nodes which are children of element_name
select all text nodes which are descendents of parent
select element
nodes whose attribute id
is foo
.
this is a bit complex but composed selection from mentioned selectors
No comments:
Post a Comment