2.3 Using the Element Called element

Literal result elements aren't the only way to create elements on the result tree. You can also use the XSLT instruction element. The following document, element.xml, is similar to literal.xml, which you saw earlier in this chapter:

<?xml version="1.0"?>    <message>You can use the element element to create elements on the result tree. </message>

Unlike literal.xsl, the stylesheet element.xsl uses element instead of a literal result element to create a new element in the output:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes"/>     <xsl:template match="message">    <xsl:element name="{concat('my', name(  ))}"><xsl:apply-templates/></xsl:element>  </xsl:template>    </xsl:stylesheet>

element has three attributes. The name attribute is required as it obviously specifies a name for the element. In this example, the name attribute uses an attribute value template to compute a name for the element. In other words, the name of the element is computed by using the concat( ) and name( ) functions to contrive a new name based on the name of the current node. This is useful when you don't have the name of a node until you actually perform the transformation (at runtime).

You don't have to use an attribute value template in the value of name you could use any legal XML name you want in the value. Computing the name, however, is one justification for using element. Another justification is using attribute sets, which you'll learn about presently. Otherwise, you might as well use a literal result element, but the choice remains yours.

2.3.1 The namespace attribute

element has two other attributes beside name: namespace and use-attribute-sets, which are optional. I'll discuss namespace here, and I'll explain how to work with use-attribute-sets in Section 2.4.1, a little later in this chapter.

The namespace attribute identifies a namespace name to associate with the element. If element's name attribute contains a QName with a prefix, the processor will usually associate the namespace name in the namespace attribute with the prefix in the QName, though it is not required to do so (see Section 7.1.2 of the XSLT spec). You can use either a namespace URI in namespace or you can compute the namespace with an attribute value template. The stylesheet namespace.xsl uses a namespace URI:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes"/>    <xsl:template match="/">  <xsl:element name="doc:paragraph" namespace="http://www.example.com/documents">   <xsl:apply-templates/>  </xsl:element> </xsl:template>    </xsl:stylesheet>

Apply this stylesheet to element.xml:

xalan element.xml namespace.xsl

and you will see what I'm talking about:

<?xml version="1.0" encoding="UTF-8"?> <doc:paragraph xmlns:doc="http://www.example.com/documents">You can use the element element to create elements on the result tree.</doc:paragraph>

When the XSLT processor encounters the namespace name http://www.example.com/documents in namespace and the QName doc:paragraph in name, it associates the prefix doc with the namespace name http://www.example.com/documents in the namespace declaration, as you can see. (I should say it usually associates the doc prefix with the namespace URI, unless there is a clash.)

Likewise, if you declare this namespace name and prefix on the document element in the stylesheet, as in rootns.xsl:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:doc="http://www.example.com/documents"> <xsl:output method="xml" indent="yes"/>     <xsl:template match="/">   <xsl:element name="doc:paragraph"><xsl:apply-templates/></xsl:element>  </xsl:template>    </xsl:stylesheet>

Transforming element.xml against rootns.xsl using:

xalan element.xml rootns.xsl

will produce the same result as transforming element.xml against namespace.xsl:

<?xml version="1.0" encoding="UTF-8"?> <doc:paragraph xmlns:doc="http://www.example.com/documents">You can use the  element element to create elements on the result tree.</doc:paragraph>

This section has only covered a few basics about element. You will get to see element at work in a larger example in the later section, Section 2.7. Now let's add an attribute or two to the paragraph element with the attribute instruction.



Learning XSLT
Learning XSLT
ISBN: 0596003277
EAN: 2147483647
Year: 2003
Pages: 164

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net