Browse DevX
Sign up for e-mail newsletters from DevX


Programmatically Apply XSLT in a Dynamic Java Application-2 : Page 2




Building the Right Environment to Support AI, Machine Learning and Deep Learning

What is XSLT?
XSLT provides an elegant, yet powerful mechanism for dynamically modifying the format, content, and organization of XML documents. Because XSLT is, itself, defined using XML, it also is a vendor-, platform-, and language-neutral standard, making it very flexible. Its variety of uses include:

  • Serving data to clients with different formatting requirements (WML, XML, HTML, pure text, etc.)
  • Extracting certain data from a large XML stream or file
  • Displaying information conditionally (based upon weather, time of day, security privileges, etc.)
  • Seamlessly integrating content from multiple sources
  • Maintaining a flexible display that can be easily modified

It also supports many advanced transformation features, such as conditional logic, loops, and a very powerful template-matching mechanism. All the transformation instructions are defined in a single XSL file. When you want to apply a particular XSL stylesheet to an XML file, simply declare this instruction with the following line:

<?xml-stylesheet type="text/xsl" href="MyStylesheet.xsl"?>

XSLT applies transformation rules to parts of the XML input tree and transforms it into a different XML output tree. The language uses a template-driven mechanism for defining and applying these transformations. Each template consists of a set of transformations that it applies to all or part of an XML node, and the template indicates which node it applies to by specifying that it matches the name of a particular XML element. This element will then serve as the root node for the transformations defined within that template.

Basic XSLT Syntax
Every XSLT stylesheet begins with a header section, which contains the file's XML declaration, the root element, and a namespace declaration. Thus, your stylesheets should always follow this pattern:

<?xml version= "1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"> <!-- XSLT transformations contained here --> </xsl:stylesheet>

One or more XSL templates (indicated by the <xsl:template> tag) live between the <xsl:stylesheet> tags. The data from the XML file will be inserted into these templates. The template element includes an optional match attribute, which enables it to target a specific node to transform within the XML document. At least one template in every stylesheet must include a match attribute specifying either the value "/" or the name of the root element. This indicates which template should serve as the starting point for applying transformations.

To invoke one template from another, the <xsl:apply-templates/> tag causes all matching templates to be processed and their output to be inserted into the output tree at that exact point. This tag supports a select attribute, which allows the tag to specify a particular template rather than apply all templates.

<xsl:template match="/">
       <h2>This is a dynamic page generated by an
XSLT Stylesheet</h2> <hr> <h3>Here comes someElement</h3> <xsl:apply-templates select="someElement"/> <hr> <h3>Here comes otherElement</h3> <xsl:apply-templates select="otherElement"/> </BODY> </HTML> </xsl:template>

A variety of other templates are called from the main template (the root template indicated by name or with a "/"). In this situation, a template very often will be matched to a particular type of element that appears multiple times. This means that template will be called once for each element of that type. Utilizing the <xsl:value-of> tag to insert dynamic values from the XML input tree into the XML output tree template is often desirable. The <xsl:value-of> tag includes a select attribute through which an XML element name for the current node (determined based upon the template match) can be matched and the value extracted at runtime:

<xsl:template match="tableElement">
    <td><xsl:value-of select="NAME"/></td>
    <td><xsl:value-of select="AGE"/></td>

Thanks for your registration, follow us on our social networks to keep up-to-date