Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

XSL gives your XML some style

Use XSL and servlets to style your XML data

  • Print
  • Feedback

Page 2 of 7

xmlns:java="http://xml.apache.org/xslt/java"


Pattern matching

When selecting in a stylesheet, a pattern is used to denote which element or attribute we want to access. The syntax is simple: specify the node you want, using / to separate the elements.

Notice that in the sample XML code above we matched our template on /, which is the root node. We could have, however, matched on the employee node. Then a select statement could just refer to the name node instead of employee/name.

For example, if we had the following XML:

<employee id="03432">
  <name>Joe Shmo</name>
  <title>Manager</title>
</employee>


Attributes can also be selected. The employee id could be accessed by saying employee/@id. Groups of nodes can be accessed by using employee/*. A specific employee could be located using employee/@id='03432'.

Pattern matching allows us to select specific values out of our XML document. The <xsl:value-of select... command gives us the ability to select a value for our resulting XML document, as seen in the table below.


Accessing elements versus attributes
Command Result
<xsl:value-of select="employee/name"/> Joe Shmo
<xsl:value-of select="employee/@id"/> 03432

Templates



Templates provide a way to match nodes in an XML document and perform operations on them. The syntax for a template is:

<xsl:template match="nodename">
.
.
.
</xsl:template>


The template is matched on a node name, then all the stylesheet commands in that template are applied. We can call templates in our stylesheet by using the apply-templates command:

<xsl:apply-templates select="nodename"/>


An example using our employee XML above would be:

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


We can call this template anywhere there is a name node to be referenced, using this:

<xsl:apply-templates select="name"/>


Logical commands

There are a few structures available for doing ifs and loops. Let's take a look at the syntax.

Choose command

The choose command provides a structure to test different situations.

<xsl:choose>
  <xsl:when test="test situation">
    stylesheet commands
  </xsl:when>
  <xsl:otherwise>
    stylesheet commands
  </xsl:otherwise>
</xsl:choose>


The first successful test will result in that block's stylesheet commands executing. If all the tests fail, the otherwise block is executed. You may have as many when blocks as you want. The otherwise block must always be present; if you don't want to do anything in your otherwise block, just put:

<xsl:otherwise/>


If command

The if command provides only a single test and doesn't have any kind of else structure available. If you need to have an else, use the choose command.

<xls:if test="test situation">
...
</xsl:if>


Loops (for-each command)

Unlike most languages with for and while structures, XSL offers only the for-each command. As such, you can loop on a set of nodes or you can select the nodes you want to loop on, using a pattern match:

<xsl:for-each select="select statement">
...
</xsl:for-each>


For example, if you had more than one employee in your XML document, and you wanted to loop through all the managers, you could use a statement such as this:

  • Print
  • Feedback

Resources