Tyrannosaurus: XSLT Manual

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" />
  • xsl:apply-templates
  • xsl:attribute
  • This directive assigns an attribute to the wrapping tag.

    AttributeRequiredDescription
    nameYesspecifies attribute name
    namespaceNoURI
    <TagFoo>
    <xsl:attribute name="AttrFoo">
    AttrVal 
    </xsl:attribute>
    </TagFoo>
  • xsl:apply-imports
  • xsl:attribute-set
  • xsl:choose
  • xsl:copy
  • xsl:for-each
  • xsl:decimal-format
  • xsl:element
  • xsl:fallback
  • xsl:if
  • xsl:import
  • It includes external .xsl files with a lower precedence. It must reside directly under <xsl:stylesheet> or <xsl:transform> element.

    AttributeRequiredDescriptionPosition
    hrefYesURITop level
  • xsl:include
  • xsl:key
  • xsl:message
  • xsl:namespace-alias
  • xsl:number
  • xsl:otherwise
  • xsl:output
  • xsl:param
  • xsl:preserve-space
  • xsl:strip-space
  • xsl:stylesheet
  • xsl:template
  • xsl:transform
  • xsl:text
  • It writes texts to output. If escaping is disabled, angle brackets will be rendered as it is.

    AttributeRequiredValueDefault
    disable-output-escapingNoyes | nono
  • xsl:value-of
  • xsl:variable
  • xsl:when
  • xsl:with-param
  • xsl:

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.

  • *
  • select all the nodes

  • prefix:*
  • select all the nodes from the namespace prefix

  • @*
  • select all nodes who have attribute

  • text()
  • select any text node

  • node()
  • select all nodes except the root

  • element_name
  • select the node with specified tag name

  • child::element_name
  • select any public node (doesn't belongs to any namespace)

  • element_name1 | element_name2 | @id
  • selector operator or

  • element_name/text()
  • select all text nodes which are children of element_name

  • parent//text()
  • select all text nodes which are descendents of parent

  • element[@id='foo']
  • select element nodes whose attribute id is foo.

  • prefix:parent[@id='foo']/element_name
  • this is a bit complex but composed selection from mentioned selectors

No comments:

Post a Comment

Myriad

Visitors