WebReference.com - Excerpt from Inside XSLT, Chapter 2, Part 5 (3/4) | WebReference

WebReference.com - Excerpt from Inside XSLT, Chapter 2, Part 5 (3/4)

To page 1To page 2current pageTo page 4
[previous] [next]

Inside XSLT

Output Method: XML

In this section I'm going to use an example that you'll see more about in Chapter 6. I'm going to look ahead and use the <xsl:copy> element, which you'll see in Chapter 3, to create a stylesheet that just makes a copy of any XML document.

I use the match pattern "*", which, as mentioned earlier, matches any element, and use the <xsl:copy> element to copy the current element to the output document. This is what the new stylesheet, which just copies the source document to the result document, looks like:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="*">
    <xsl:copy>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

Because this stylesheet is for copying any XML document to a new XML document-even XHTML documents, which are XML documents that use the <html> tag-I explicitly indicate that the output method is XML here. If I didn't do this, copied XHTML documents would not start with the XML declaration:

<?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="*">
    <xsl:copy>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

This example copies only elements to the result document, not text nodes, comments, or attributes. You'll see a more complete version of this same stylesheet in Chapter 4.

Remember that XML is the default output method, unless your input document contains an <HTML> or <html> tag. However, even if you are transforming from one XML document to another, it's often useful to use the <xsl:output> element to specify, for example, the character encoding (the default is usually UTF-8, the eight-bit Unicode subset), or whether the output document should be indented (which is covered in Chapter 3).

Working with XML Fragments
You can even work on XML fragments, not just entire XML documents. In that case, you can set the omit-xml-declaration attribute to "yes" to omit the XML declaration at the beginning of the output tree, as discussed in Chapter 6.

When you use the XML output method, the output tree is well-formed XML (but there is no requirement that it be valid). There is no requirement that it be a well-formed XML document; it could be an XML external general parsed entity. The content of the output can contain character data, CDATA sections, entity references, processing instructions, comments, and elements. The output must also conform to the XML namespaces declaration.


To page 1To page 2current pageTo page 4
[previous] [next]

Created: October 11, 2001
Revised: October 11, 2001


URL: http://webreference.com/authoring/languages/xml/insidexslt/chap2/5/3.html