Figure 1 XML Source Document
<?xml version="1.0"?>
<product title="Essential XML" xmlns="https://awl.com">
<writer name='Don Box' />
<writer name='Aaron Skonnard'/>
<writer name='John Lam'/>
</product>
Figure 2 DOM-based Translation
import org.w3c.dom.*;
Document transform(Document source) throws Exception {
String tns = "https://www.develop.com/Schemas/book";
DOMImplementation dom = source.getImplementation();
Document target = dom.createDocument(tns, "content",
null);
Element sourceRoot = source.getDocumentElement();
String title = sourceRoot.getAttribute("title");
Element e1 = target.createElementNS(tns, "name");
e1.appendChild(target.createTextNode(title));
target.getDocumentElement().appendChild(e1);
e1 = target.createElementNS(tns,"contributors");
boolean bFirst = false;
for (Node author = sourceRoot.getFirstChild();
author != null; author = author.getNextSibling()) {
if (author.getNodeType() != Node.ELEMENT_NODE)
continue;
String name = ((Element)author).getAttribute("name");
Element e2 = target.createElementNS(tns, "staff");
e2.appendChild(target.createTextNode(name));
if (!bFirst)
e2.setAttributeNS("", "principal", "true");
e1.appendChild(e2);
bFirst = true;
}
target.getDocumentElement().appendChild(e1);
return target;
}
Figure 3 XSLT Translation
<?xml version="1.0"?>
<content xmlns="https://www.develop.com/Schemas/book"
xmlns:xsl="https://www.w3.org/1999/XSL/Transform"
xsl:exclude-result-prefixes='src'
xsl:version='1.0'
xmlns:src="https://awl.com">
<name><xsl:value-of select="/src:product/@title"/></name>
<contributors>
<xsl:for-each select='/src:product/src:writer'>
<xsl:if test='position() = 1' >
<staff principal='true'
><xsl:value-of select="@name"/></staff>
</xsl:if>
<xsl:if test='position() > 1'>
<staff><xsl:value-of select="@name"/></staff>
</xsl:if>
</xsl:for-each>
</contributors>
</content>
Figure 5 XSLT Instructions
Instruction |
Syntax |
Description |
xsl:copy-of |
|
Emits the node-set corresponding to the select expression. |
xsl:value-of |
|
Emits the string corresponding to the select expression. |
xsl:if |
|
Evaluates the template if and only if the test expression evaluates to true. |
xsl:choose |
|
Evaluates the template from the first xsl:when clause whose test expression evaluates to true. If none of the test expressions evaluate to true, then the template contained in the xsl:otherwise clause is evaluated. |
xsl:for-each |
|
Evaluates the template against each node in node-set returned by the select expression. The order of evaluation can be influenced using one or more xsl:sorts. |
xsl:call-template |
|
Invokes the template rule named by name. |
xsl:variable |
|
Declares a variable named name and initializes it using the select expression or template. |
xsl:text |
|
Emits the text found in #PCDATA. Escaping of the five built-in entities is controlled using disable-output-escaping. |
xsl:number |
|
Emits a number based on the XPath number expression found in value. |
xsl:copy |
|
Copies the current context node (and associated namespace nodes) to the result tree fragment. |
xsl:apply-templates |
|
Invokes the best-match template rules against the node-set returned by the select expression. |
xsl:apply-imports |
<xsl:apply-imports /> |
Promotes the current stylesheet in import precedence. |
xsl:message |
|
Emits a message in a processor-dependent manner. |
xsl:fallback |
|
Evaluates the template when the parent instruction/directive is not supported by the current processor. |
xsl:comment |
|
Emits an XML comment containing the template as its character data. |
xsl:processing-instruction |
|
Emits an XML processing instruction whose [target] is name and whose [children] are based on template. |
xsl:element |
|
Emits an XML element whose [local name] is name, whose [namespace URI] is namespace, and whose [children] are based on template. |
xsl:attribute |
|
Emits an XML attribute whose [local name] is name, whose [namespace URI] is namespace, and whose [children] are based on template. |
Figure 6 Using Attribute Sets
<xsl:stylesheet version='1.0'
xmlns:xsl='https://www.w3.org/1999/XSL/Transform' >
<xsl:attribute-set name='myAttrs' >
<xsl:attribute name='a'>a-val</xsl:attribute>
<xsl:attribute name='b'>b-val</xsl:attribute>
</xsl:attribute-set>
<xsl:attribute-set name='yourAttrs' >
<xsl:attribute name='c'>c-val</xsl:attribute>
</xsl:attribute-set>
<xsl:template name='elems2' >
<xsl:element name='bob'
use-attribute-sets='myAttrs yourAttrs' >
<steve xsl:use-attribute-sets='yourAttrs' />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Figure 7 Employing xsl:include
<?xml version='1.0' ?>
<!-- stylesheeta.xsl -->
<xsl:stylesheet version='1.0'
xmlns:xsl='https://www.w3.org/1999/XSL/Transform'>
<xsl:template name='func-a' ><a/></xsl:template>
</xsl:stylesheet>
<?xml version='1.0' ?>
<!-- stylesheetb.xsl -->
<xsl:stylesheet version='1.0'
xmlns:xsl='https://www.w3.org/1999/XSL/Transform'>
<xsl:include href='stylesheeta.xsl' />
<xsl:template name='func-b' >
<xsl:call-template name='func-a' />
</xsl:template>
</xsl:stylesheet>
Figure 8 xsl:import Stylesheets
<?xml version='1.0' ?>
<!-- root.xsl -->
<xsl:stylesheet version='1.0'
xmlns:xsl='https://www.w3.org/1999/XSL/Transform'>
<xsl:import href='first.xsl' />
<xsl:import href='second.xsl' />
<xsl:template name='func-b' >
<xsl:call-template name='func-a' />
</xsl:template>
</xsl:stylesheet>
<?xml version='1.0' ?>
<!-- first.xsl -->
<xsl:stylesheet version='1.0'
xmlns:xsl='https://www.w3.org/1999/XSL/Transform'>
<xsl:template name='func-a' ><first/></xsl:template>
</xsl:stylesheet>
<?xml version='1.0' ?>
<!-- second.xsl -->
<xsl:stylesheet version='1.0'
xmlns:xsl='https://www.w3.org/1999/XSL/Transform'>
<xsl:import href='third.xsl' />
<xsl:template name='func-a' ><second/></xsl:template>
</xsl:stylesheet>
<?xml version='1.0' ?>
<!-- third.xsl -->
<xsl:stylesheet version='1.0'
xmlns:xsl='https://www.w3.org/1999/XSL/Transform'>
<xsl:template name='func-a' ><third/></xsl:template>
</xsl:stylesheet>