"element" and "attribute" - Constructing Output Elements

This section provides a tutorial example on how to generate transformation output in XML format. The second approach is to construct new elements using XSLT 'element' and 'attribute' elements in the transformation template.

As mentioned in the previous section, the second way to generate output XML elements is to use "element", "attribute", "text", and "comment" elements to construct new elements in the transformation template.

"element": A child element used as part of the content of a "template" element. An "element" element serves as an output statement, which will be evaluated to generate an XML element. The syntax of the "element" element is:

<element name="element_name">
 content
</element>

where "element_name" is used to define the name of the element to be generated, and "content" contains other XSLT elements or literals to define attributes, child elements and/or text content of the element to be generated.

"attribute": A child element used inside an "element" element. An "attribute" element serves as an output statement, which will be evaluated to generate an attribute inside its parent element. The syntax of the "attribute" element is:

<attribute name="attribute_name">
 attribute_value
</attribute>

where "attribute_name" is used to define the name of the attribute to be generated, and "attribute_value" contains other XSLT elements or literals to define the value of the attribute to be generated.

"text": A child element used inside an "element" element. An "text" element serves as an output statement, which will be evaluated to generate a text child inside its parent element. The syntax of the "text" element is:

<text>
 content
</text>

where "content" is used to define the content of the text child to be generated.

"comment": A child element used inside an "element" element. An "comment" element serves as an output statement, which will be evaluated to generate a comment inside its parent element. The syntax of the "comment" element is:

<comment>
 content
</comment>

where "content" is used to define the content of the comment to be generated.

Let's see an example of using "element", "attribute", "text" and "comment" elements to construct output XML elements.

Here is the source XML document, output_xml.xml:

<?xml version="1.0"?>
<user name="Herong">
 <role>administrator</role>
 Administrator can delete users.
 <point>100</point>
 Points are earned by posting messages.
</user>

Here is the transformation template that constructs some new elements:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"/>
<xsl:template match="user">
 <xsl:element name="profile">
  <xsl:attribute name="role">operator</xsl:attribute>
  <xsl:element name="rank">2</xsl:element>
  <xsl:text>Rank can be demoted.</xsl:text>
  <xsl:element name="name">Jack</xsl:element>
  <xsl:text>Name is optional.</xsl:text>
  <xsl:comment>End of profile</xsl:comment>
 </xsl:element>
</xsl:template>
</xsl:stylesheet>

Run XSLTransformer.java to perform the transformation, you should get:

<?xml version="1.0" encoding="UTF-8" ?>
<profile role="operator">
 <rank>2</rank>
 Rank can be demoted.
 <name>Jack</name>
 Name is optional.
 <!-- End of profile
 -->
</profile>

The output looks good. All 3 XML elements are constructed in the result XML document correctly.

Table of Contents

 About This Book

 Introduction of XML (eXtensible Markup Language)

 XML File Syntax

 XML File Browsers

 XML-JSON Document Conversion

 DOM (Document Object Model) Programming Interface

 SAX (Simple API for XML) Programming Interface

 DTD (Document Type Definition) Introduction

 Syntaxes of DTD Statements

 Validating an XML Document against the Specified DTD Document Type

 XSD (XML Schema Definition) Introduction

 Syntaxes of XSD Statements

 Validating XML Documents Against Specified XML Schemas

 XSL (Extensible Stylesheet Language) Introduction

 Java Implementation of XSLT

 XSLT (XSL Transformations) Introduction

 XPath (XML Path) Language

 XSLT Elements as Programming Statements

Control and Generate XML Element in the Result

 "output" - The Output Format Control Element

 Generating Transformation Output in XML Format

"element" and "attribute" - Constructing Output Elements

 "copy" - Copy Elements from Source to Result

 Copying Attributes and Texts

 PHP Extensions for XML Manipulation

 Processing XML with Python Scripts

 XML Notepad - XML Editor

 XML Tools Plugin for Notepad++

 XML Plugin Packages for Atom Editor

 XML 1.1 Changes and Parsing Examples

 Archived Tutorials

 References

 Full Version in PDF/EPUB