TEMPLATES examples

  • XSL Explanation
  • Templates -- built-in templates, adding modularity, includes and imports
  • XSL Namespaces-- using namespaces with XSL
  • XPath -- getting around XSL
  • XPath Examples
  • Functions-- using functions
  • Functions Examples
  • Variables and Parameters
  • basic template
    <?xml version="1.0"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    connecting, set up which version and where to get our xsl processing instructions from

    namespace xsl is not defaulted, but must be specified here

    <xsl:template match="/">

    • opening of template
    • match tells our browser where to look for elements to add to our result tree
    • the "/" stands for the document root
    • so, it starts at the beginning of our xml document
    • then it will go to the instructions directly below it

    <xsl:value-of select="." />

     
    </xsl:template> closing of template
    </xsl:stylesheet> closing of stylesheet
    example <xsl:apply-templates />
    <?xml version="1.0"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    our connection
    <xsl:template match="*"> * is a wild card, gets everything
    <xsl:apply-templates /> tells processor to apply any templates
    </xsl:template> closes this template that gets everything
    <xsl:template match="text()"> text() only works for text nodes or nodes that contain zero subelements, since we are at the root element, nothing applies
    <div style="border: solid 1px"> skips this
    <xsl:value-of select="." /> skips this
    </div> skips this
    </xsl:template> closes this template
    <xsl:template match="/"> now, the template gets all the nodes
    <div style="border: solid 3px red; padding: 10px;">  
    <xsl:value-of select="."> sends everything to the browser in one big clump -- if this tag is used
    <xsl:apply-templates/>

    or if we use this tag, we tell the browser to on and see if there are any onther templates that apply.

    So, it gets down to our text() nodes and applies the template for the text nodes.

    </div>
    </xsl:template>
    </xsl:stylesheet>
    close
    example get text of our context node
    <?xml version="1.0"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    our connection
    <xsl:template match="*"> * is a wild card, gets everything
    <xsl:apply-templates /> tells processor to apply any templates
    </xsl:template> closes this template that gets everything
    <xsl:template match="text()"> if our node has text
    <xsl:apply-templates /> then we will apply our templates
    </xsl:template /> close this template
    <xsl:template match="vendor_name">

    locate what we are looking for, in this case:

    "vendor_name"

    <p> closes this template
    <xsl:value-of select="." /> now, the template gets all the nodes
    </p>
    </xsl:template>
    </xsl:stylesheet>
    close
    List of templates:
    predicate brace notation is used, so that contents inside the braces are evaluated as if they were XPath expressions.
    attribute value template

    if brace notation is mixed with text, use with <xsl:attribute>,<xsl:document>,<xsl:processing>

    cannot evaluate to an XML tree fragment -- they convert to a string, use <xsl:variable name="node_redundant" select="msxml:node-set($temp_redundant)"/> to convert to a node-set, see variables:Nodeset for example

    apply-imports
    1. overrule a standard template that you imported using <xsl:import>
    2. pass context on to another template
      • select which nodes to transform
      • mode (optional) -- matches and processes only templates with the same mode attribute
    apply-templates looks for matching templates to use
    changes the context node to the next node
    attribute generates an attribute in the destination document (used with an element)
    attribute-set add a group of attributes, use name="xxx" to define the group
    use-attribute-sets add to an existing attribute set
    boolean element-available(string) query availability of a certain extension element
    boolean function-available(string) query availability of a certain extension function
    call call a template by name
    doesn't change the context node
    Can use the same functionality in several templates
    choose compare to case/select in VB or switch in java
    comment generates a comment node in the destination document
    copy generates a copy of the context node -- no children or attributes
    copy-of copies full tree, (attributes and children) to destination.
    if multiple nodes are matched by the select attribute - all subtrees are copied
    decimal controls the interpretation of a format pattern used by the format-number() function
    attributes:
    name, decimal-separator, grouping-separator, infinity, minus-sign, NaN, percent, per-mille, zero-digit digit, pattern-separator
    document attributes:
    method: xml, html, text
    cdata-section-elements: specifies a list of elements that should have their content escaped by using CDATA section instead of entities
    version, encoding, omit-xml-declaration, standalone, doctype-public, doctype-system, indent, media-type
    element name (required), namespace (optional), use-attribute-sets (adds attribute sets to the element)
    fallback specify actions to be executed if the action of its parent element is not supported by the processor
    for-each loops with select attribute
    if  
    import href
    imports the templates from an external stylesheet document into the current document
    include href
    includes templates from an external document as if they were part of the importing document -- same priority (don't use the same "match" and "priority" attributes in both the including and included stylesheets.
    key name -- references this key (required)
    match -- matches the element name (required)
    use-attribute -- matches the attribute (required)
    key() -- pass the value of the attribute needed (required)
    create an index-like structure that can be queried from the key() function
    like a dictionary object in VB, hashtable in java, or associative array in perl
    more than one value can be found for one key and the same value can be accessed by multiple keys
    message terminate (if = "yes", execution of transformation is stopped)
    namespace stylesheet-prefix (required) (variable name that identifies each separate namespace)
    number
      outputs the number of a paragraph or chapter in a specified format
      level = "single", "multiple", "any"
      count (specifies type of node to count
      from - specifies starting point for counting
      value - numeric value to specify the value, insted of using "level" and "count" and "from"
      format -- changes the numeric value to a string (i.e. a, b, c)
      lang - language for alphabetic numbering
      group-separator -- character to be used for group separation
      grouping-size -- number of digits to be separated
        grouping-separator=";" grouping-size="3" causes 1;000;000
    node-set context(number) returns the context node-set, enter a number to navigate the tree
    node-set current() returns the current context node-set
    node-set document(object, node-set?) get a reference to an external source document (if this is a string, you set the URL of the document to be retrieved), If a node-set, all nodes are converted to strings and all these URLs are retrieved in a node-set)
    node-set generate-id(node-set?) generates a unique identifier for specified node, same node will always generate same id, multiple transformations will not remain identical
    node-set key(string, node-set) to get a reference to a node using the <xsl:key>, use the name of the <xsl:key>
    if of type node-set, all nodes are converted to strings, and all are used to get nodes back from the key
    node-set unparsed-entity-url (string) returns the URI of the unparsed entity with the passed name
    object system-property  
    otherwise executes only if <xsl:when> in a <xsl:choose> are not matched
    output
      output style of destination document
      method
        xml is default; html -- creates empty elements like <br> and use entities like &agrave;(à), text -- returns just text, no entity references in output
      version, encoding, omit-xml-declaration, standalone, doctype-public, doctype-system, indent, media-type
      cdata-section-elements -- lists which elements should be escaped by cdata insted of entities
    param name -- name of parameter
    select -- select default value of parameter
    preserve-space preserve whitespace, see also <xsl:strip-space>
    processing name
    generates a processing instruction in the destination document
    sort specifies a sort order for <xsl:apply-templates> and <xsl:for-each> elements
    select, lang, data-type (specify alphabetic or numberic ordering -- "text", "number" order="ascending" "descending", case-order="upper-first" or "lower-first"
    strip-space get rid of whitespace
    stylesheet root element for stylesheet, could also use <xsl:transform>
    template match, name, priority
    mode -- the template will only be considered for transforming a node when the transformation was started by an <xsl:apply-templates> element with a mode attribute of the same value
    text generates text
    disable-output-escaping="yes" output will not be escaped (< will be written to output as "<", not &lt;, not well-formed xml anymore)
    transform <xsl:stylesheet>
    id, extension-element-prefixes, exclude-result-prefixes, version
    value-of Only gets the first node -- you need <for-each> to get all of the value-of's
    gets text string, or attribute value if @attrname is used
    select, disable-output-escaping
    variable

    define a variable with a value
    name -- name the variable
    select -- value of the variable (if select is omitted, the content of the variable is the value and cannot be changed)

    use $ to refer to the declared variable

    example: <xsl:variable name="x" select="/parent/child" />
    <xsl:variable name="y" select="$x/@age" />

    when an option for the <xsl:choose> block
    test -- expression to be tested
    with pass a parameter to a template using <xsl:apply-templates> or <xsl:call-template>
    the template called must have a parameter of the same name defined using <xsl:param>
    name -- name of the parameter, select -- xpath expression selecting the passed value

    templates to generate xml constructs directly
    xsl:attribute
    xsl:comment
    xsl:element
    xsl:processing-instruction
    xsl:text