RXSLT Examples The following examples are all the examples in the XSLT 1.0 specification, each followed by the corresponding RXSLT program or program fragment, and by the output of the RXSLT-to-XSLT translator in three forms: preserving all space (non-indented), wrapping all text in elements (indented), and wrapping in only when necessary (better indented). (Where the latter two give the same output, which is often the case, the "better indented" output is omitted.) The examples are as in the XSLT 1.0 spec except where some small modification has to be made to make it complete. This file was produced automatically by running it through the RXSLT compiler. As a result, all XSLT 1.0 outputs are encoded as full stylesheets even where the XSLT 1.0 spec example is just a code fragment. `h:2.3 Literal Result Element as Stylesheet XML-encoded XSLT: Expense Report Summary

Total Amount:

RXSLT: Expense Report Summary

Total Amount: {value-of expense-report/total}

`h:2.5 Forwards-Compatible Processsing This is not really supported by other than by XML-encoding the non-XSLT 1.0 feature. XML-encoded XSLT: XSLT 1.1 required

Sorry, this stylesheet requires XSLT 1.1.

RXSLT: xslt1.1 template / choose when system-property("version") >= 1.1 otherwise XSLT 1.1 required

Sorry, this stylesheet requires XSLT 1.1.

`h:2.6.2 Stylesheet Import XML-encoded XSLT: italic RXSLT: import article.xsl import bigfont.xsl attribute-set note-style attribute font-style : "italic" `h:2.7 Embedding Stylesheets This feature is currently not supported, but could be supported by wrapping a whole RXSLT program in "style" tags in the manner that Javascript programs are embedded in HTML documents. `h:5.3 Defining Template Rules XML-encoded XSLT: RXSLT: template emph {apply-templates} `h:5.4 Applying Template Rules (first example) XML-encoded XSLT: RXSLT: template chapter {apply-templates} `h:5.4 Applying Template Rules (second example) XML-encoded XSLT: RXSLT: template author-group {apply-templates with-nodes author} `h:5.4 Applying Template Rules (third example) XML-encoded XSLT: RXSLT: template author-group {apply-templates with-nodes author/given-name} `h:5.4 Applying Template Rules (fourth example) XML-encoded XSLT: RXSLT: template book {apply-templates with-nodes .//heading} `h:5.4 Applying Template Rules (fifth example) XML-encoded XSLT: Employee belongs to group RXSLT: template employee Employee {apply-templates with-nodes name} belongs to group {apply-templates with-nodes ancestor::department/group} `h:5.4 Applying Template Rules (sixth example) XML-encoded XSLT:
RXSLT: template product {apply-templates with-nodes sales/domestic}
{apply-templates with-nodes sales/foreign}
`h:5.4 Applying Template Rules (seventh example) XML-encoded XSLT: RXSLT: template doc apply-templates with-nodes .//div `h:5.4 Applying Template Rules (eighth example -- the invalid one) XML-encoded XSLT: RXSLT: template foo apply-templates with-nodes . `h:5.6 Overriding Template Rules file "doc.xsl": XML-encoded XSLT:
RXSLT: template example
{apply-templates}
`hide`end importing stylesheet: XML-encoded XSLT:
RXSLT: import "doc.xsl" template example
{apply-imports}
`h:5.8 Built-in Template Rules (element and root nodes) XML-encoded XSLT: RXSLT: template * | / apply-templates `h:5.8 Built-in Template Rules (for each mode) XML-encoded XSLT: RXSLT: mode m template * | / apply-templates `h:5.8 Built-in Template Rules (text and attribute nodes) XML-encoded XSLT: RXSLT: template text() | @* value-of . `h:5.8 Built-in Template Rules (processing instructions and comments) XML-encoded XSLT: RXSLT: template processing-instruction() | comment() `h:7.1.1 Literal Result Elements XML-encoded XSLT: RXSLT: xslt1.0 xmlns fo="http://www.w3.org/1999/XSL/Format" xmlns axsl="http://www.w3.org/1999/XSL/TransformAlias" stylesheet-prefix axsl result-prefix xsl template / {apply-templates} template block `h:7.1.3 Creating Attributes with xsl:attribute XML-encoded XSLT: x y RXSLT: attribute a : "x y" `h:7.1.4 Named Attribute Sets XML-encoded XSLT: 12pt bold RXSLT: template chapter/heading {apply-templates} attribute-set title-style attribute font-size : "12pt" attribute font-weight : "bold" `h:7.3 Creating Processing Instructions XML-encoded XSLT: href="book.css" type="text/css" RXSLT: processing-instruction xml-stylesheet : 'href="book.css" type="text/css"' `h:7.4 Creating Comments XML-encoded XSLT: This file is automatically generated. Do not edit! RXSLT: comment: "This file is automatically generated. Do not edit!" `h:7.5 Copying (first example) XML-encoded XSLT: RXSLT: template @* | node() copy apply-templates with-nodes @* | node() `h:7.5 Copying (second example -- definition) XML-encoded XSLT: RXSLT: named-template apply-templates-copy-lang for-each @xml:lang copy apply-templates `h:7.5 Copying (second example -- use) XML-encoded XSLT: RXSLT: call-template apply-templates-copy-lang `h:7.6.1 Generating Text with xsl:value-of (first example) XML-encoded XSLT:

RXSLT: template person

{value-of @given-name; " "; value-of @family-name}

`h:7.6.1 Generating Text with xsl:value-of (second example) XML-encoded XSLT:

RXSLT: template person

{value-of given-name; " "; value-of family-name}

`h:7.6.1 Generating Text with xsl:value-of (third example) XML-encoded XSLT: RXSLT: template procedure {value-of ancestor-or-self::*[@security][1]/@security} apply-templates `h:7.6.2 Attribute Value Templates (first example) XML-encoded XSLT: /images RXSLT: variable image-dir "/images" template photograph `h:7.7 Numbering (numbering a sorted list) XML-encoded XSLT:

RXSLT: template items for-each item sort using .

{number value=position() format="1. " value-of .}

`h:7.7 Numbering (numbering items in an ordered list) XML-encoded XSLT: . RXSLT: template ol/item {number; ". "; apply-templates} `h:7.7 Numbering (numbering titles) XML-encoded XSLT: RXSLT: template title {multiple-number count=chapter|section|subsection format="1.1 " apply-templates} 1 template appendix//title {multiple-number count=appendix|section|subsection format="A.1 " apply-templates} `h:7.7 Numbering (numbering notes) XML-encoded XSLT: RXSLT: template note {number from=chapter format="(1) " apply-templates} `h:7.7 Numbering (numbering H4 elements) XML-encoded XSLT: . . RXSLT: template H4 {number from=H1 count=H2 "." number from=H2 count=H3 "." number from=H3 count=H4 " " apply-templates} `h:8. Repetition XML-encoded XSLT: Customers
RXSLT: template / Customers {for-each customers/customer {for-each order } }
{apply-templates with-nodes name} {apply-templates}
`h:9.1 Conditional Processing with xsl:if (first example) XML-encoded XSLT: , RXSLT: template namelist/name apply-templates if not(position()=last()): ", " `h:9.1 Conditional Processing with xsl:if (second example) XML-encoded XSLT: yellow RXSLT: template item { if position() mod 2 = 0 attribute bgcolor "yellow" apply-templates} `h:9.2 Conditional Processing with xsl:choose XML-encoded XSLT: . RXSLT: template orderedlist/listitem {variable level = count(ancestor::orderedlist) mod 3 choose when $level=1 number format=i when $level=2 number format=a otherwise number format=1 ". "} {apply-templates} `h:10. Sorting XML-encoded XSLT:
  • RXSLT: template employees
      {apply-templates with-nodes employee sort using name/family sort using name/given }
    template employee
  • {value-of name/given; " "; value-of name/family}
  • `h:11.2 Values of Variables and Parameters (various examples) XML-encoded XSLT: 2 RXSLT: variable x variable x = '' variable n = 2 value-of item[$n] variable n : "2" value-of item[position()=$n] param x = /.. `h:11.4 Top-level Variables and Parameters XML-encoded XSLT: 12pt RXSLT: variable para-font-size : "12pt" template para {apply-templates} `h:11.5 Variables and Parameters within Templates (the allowed version) XML-encoded XSLT: RXSLT: param x = 1 named-template foo variable x = 2 `h:11.6 Passing Parameters to Templates XML-encoded XSLT: 1. a. RXSLT: named-template numbered-block param format : "1. " {number format="{$format}" apply-templates} template ol//ol/li call-template numbered-block with-param format : "a. " `h:12.2 Keys (div element with attribute id) XML-encoded XSLT: RXSLT: key idkey match div use @id `h:12.2 Keys (prototype element) XML-encoded XSLT:

    Function: ...

    RXSLT: key func match prototype use @name template function {apply-templates} template prototype

    Function: ...

    `h:12.2 Keys (from another document) XML-encoded XSLT: RXSLT: key bib match entry use @name template bibref variable name = . for-each document('bib.xml') apply-templates with-nodes key('bib',$name) `h:12.4 Miscellaneous Additional Functions XML-encoded XSLT: RXSLT: value-of current() value-of . apply-templates with-nodes //glossary/item[@name=current()/@ref] apply-templates with-nodes //glossary/item[@name=./@ref] apply-templates with-nodes //glossary/item[@name=@ref] `h:13 Messages XML-encoded XSLT: problem RXSLT: param lang = en variable messages = document(concat('resources/', $lang, '.xml'))/messages named-template localized-message param name message : value-of $messages/message[@name=$name] named-template problem call-template localized-message with-param name : "problem" `h:16.1 XML Output Method XML-encoded XSLT: RXSLT: output cdata-section-elements=example `h:16.2 HTML Output Method XML-encoded XSLT: ... RXSLT: output method=html template / {apply-templates} `h:16.4 Disabling Output Escaping XML-encoded XSLT: < RXSLT: unescaped "<" `h:D.1 Document Example XML-encoded XSLT: <xsl:value-of select="title"/>

    NOTE:

    RXSLT: xslt1.0 xmlns = "http://www.w3.org/TR/xhtml1/strict" strip-space doc, chapter, section output method=xml indent=yes encoding=iso-8859-1 template doc {value-of title} {apply-templates} template doc/title

    {apply-templates}

    template chapter/title

    {apply-templates}

    template section/title

    {apply-templates}

    template para

    {apply-templates}

    template note

    NOTE: {apply-templates}

    template emph {apply-templates} `h:D.2 Data Example (HTML outputter) XML-encoded XSLT: Sales Results By Division
    Division Revenue Growth Bonus
    color:red
    RXSLT: Sales Results By Division { for-each sales/division # order the result by revenue sort using revenue data-type="number" order="descending" }
    Division Revenue Growth Bonus
    {value-of @id} {value-of revenue} { # highlight negative growth in red if growth < 0 attribute style : "color:red" value-of growth } {value-of bonus}
    `h:D.2 Data Example (SVG outputter) XML-encoded XSLT: Revenue Division RXSLT: xslt1.0 xmlns="http://www.w3.org/Graphics/SVG/SVG-19990812.dtd" output method=xml indent=yes media-type="image/svg" template / Revenue Division { for-each sales/division # define some useful variables # the bar's x position variable pos = (position()*40)-30 # the bar's height variable height = revenue*10 # the rectangle # the text label # the bar value {value-of revenue} } `h:D.2 Data Example (VRML outputter) XML-encoded XSLT: #VRML V2.0 utf8 # externproto definition of a single bar element EXTERNPROTO bar [ field SFInt32 x field SFInt32 y field SFInt32 z field SFString name ] "http://www.vrml.org/WorkingGroups/dbwork/barProto.wrl" # inline containing the graph axes Inline { url "http://www.vrml.org/WorkingGroups/dbwork/barAxes.wrl" } bar { x y z name "" } RXSLT: # generate text output as mime type model/vrml, using default charset output method=text encoding=UTF-8 media-type="model/vrml" template / : '#VRML V2.0 utf8 # externproto definition of a single bar element EXTERNPROTO bar [ field SFInt32 x field SFInt32 y field SFInt32 z field SFString name ] "http://www.vrml.org/WorkingGroups/dbwork/barProto.wrl" # inline containing the graph axes Inline { url "http://www.vrml.org/WorkingGroups/dbwork/barAxes.wrl" } ' for-each sales/division 'bar { x '; value-of revenue; ' y '; value-of growth; ' z '; value-of bonus; ' name "'; value-of @id; '" }} '