Generating XML

So far, we've seen how you can use templates to insert new information, such as HTML tags, into the content of your XML documents. There are also some ways in which we can actually use XSLT to write elements and attributes into the output, effectively changing the XSL from one vocabulary to another.

xsl:element

The xsl:element element enables you to use a template to create a new element in your output. The element element takes three attributes:

  • name— The name attribute allows you to specify the name of the element. This either can be a straightforward name, or it can take the form of an expression. This provides you with the power to generate dynamic names for your elements based on the content of the XML document being processed.

  • namespace— This attribute can be used to establish a Namespace for your element.

  • use-attribute-sets— This attribute is used to specify an attribute-set to be appended to the element. An attribute-set is simply a grouping of attributes, which can then be used with element generation.

Let's take a look how we could use the xsl:element with our address document. Say we wanted to convert our generic phone element into a set of elements, based on the type of phone number specified in the attribute:

<xsl:template match="address_book"> 
  <xsl:apply-templates select="contact/phone"/>
</xsl:template>

<xsl:template match="number">
  <xsl:element name="{@type}">
    <xsl:value-of select="."/>
  </xsl:element>
</xsl:template>



<xsl:template match="address_book">
  <xsl:apply-templates select="contact/phone"/>
</xsl:template>

<xsl:template match="number">
  <xsl:element name="{@type}">
    <xsl:value-of select="."/>
  </xsl:element>
</xsl:template>

In the previous example, the name of the new element is actually generated by the expression "{@type}", which results in the content of the type attribute for the phone number being used as the name. The result is a new element for each separate number:

<office>708-555-1212</office> 
<mobile>708-855-4848</mobile>
<fax>800-555-1212</fax>

<office>812-555-1212</office>
<mobile>812-855-4848</mobile>
<fax>800-333-0999</fax>
						

xsl:attribute

In addition to being able to generate elements, you can also use the xsl:attribute element to generate attributes in the output. There are two attributes you can use with the xsl:attribute element:

  • name— Specifies the name of the attribute.

  • namespace— Specifies the Namespace for the attribute.

The use of the xsl:attribute follows the form of the element pretty closely; however, the attribute element must be nested within an element, or a tag pair:

<xsl:template match="contact"> 
<xsl:element name="office">
 <xsl:attribute name="phone">
  <xsl:value-of select="phone/number"/>
 </xsl:attribute>
</xsl:element>
</xsl:template>
						

xsl:attribute-set

Attribute sets are a mechanism for grouping together commonly used attributes, which can then be assigned to use with an xsl:element element using the use-attribute-set attribute.

An attribute set has two attributes:

  • name— This enables you to define a name for the attribute set.

  • use-attribute-sets— You can include other attribute sets within an attribute set by referencing an existing attribute set in this attribute. Keep in mind that it is an error for an attribute set to reference itself.

The attribute-set itself will contain any number of attribute elements. For example:

<xsl:attribute-set name="specs"> 
 <xsl:attribute name="size">
  <xsl:value-of select="item/@size"/>
 </xsl:attribute>
 <xsl:attribute name="color">
  <xsl:value-of select="item/@color"/>
 </xsl:attribute>
</xsl:attribute-set>

This attribute set could then be used with xsl:element:

<xsl:element name="shirt" use-attribute-set ="specs"> 
</xsl:element>

which will result in the created element having attributes of size and color.

Attribute sets can be a great way to group together a large number of attributes that are going to be used with a single element, or to create a group for attributes that are going to be used repeatedly.

xsl:text

The xsl:text element allows you to insert text into the output. It has only one attribute:

  • disable-output-escaping

which allows you to toggle on and off the escaping of symbols in the text stream. The use of xsl:text is straightforward:

<xsl:text>This will insert text into the output.</xsl:text> 

xsl:comment

Commenting your code is not only good style, it's an easy way to pass along messages to people who might be using your documents. The xsl:comment element enables you to insert comments into the output. For example:

<xsl:comment>This file was generated automatically with XSLT</xsl:comment> 

would result in the following comment being inserted into the final output document:

<!-- This file was generated automatically with XSLT --> 

xsl:copy

There are times when you might simply want to copy the contents of an element from one document to another without any changes. To accomplish this, you can use the xsl:copy element, which simply copies the element from the document tree to the result tree.

You can use the use-attribute-sets attribute with the xsl:copy element if you want to add an attribute set to the element during the copy.

If we wanted to copy the name element without any intervention, we could use the following template:

<xsl:template match="name"> 
  <xsl:copy>
   <xsl:apply-templates/>
  </xsl:copy>
 </xsl:template>

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.219.80