XPath Kick Start: Navigating XML with XPath 1.0 and 2.0

For the most part, we've been creating HTML documents from XML documents with XSLT. You might have noticed that the output documents are HTML, not XML, even though we haven't explicitly told the XSLT processor to make them HTML. That's because there is a special default rule hereif the document node of the output document is <HTML> , XSLT processors are supposed to treat the output document as HTML.

On the other hand, you can specifically specify the type of output document you want, using the XSLT <xsl:output> element. Here are the options:

  • XML This is the default. Makes the output documents start with an <?xml?> declaration.

  • HTML Makes the output document standard HTML 4.0, without an XML declaration.

  • Text Makes the output document simple text.

To select one of these, you set the <xsl:output> element's method attribute to "xml", "html", or "text". Say that you want to create a plain text documentin that case, you can use this <xsl:output> element:

<xsl:output method = "text"/>

You can also use the media-type attribute of <xsl:output> to specify the MIME type of the output document yourself. Here's how that might look, where we're creating a rich text format (RTF) document:

<xsl:output media-type="text/rtf"/>

Besides the method and media-type attributes, there are some other additional useful <xsl:output> attributes that let you control the output document:

  • encoding Indicates the value of the XML declaration's encoding attribute.

  • indent Indicates if the XSLT processor should indent the output (many won't, even if you ask). Set to "yes" or "no".

  • omit-xml-declaration Indicates if the processor should omit the XML declaration. Set to "yes" or "no".

  • standalone Indicates the value for the XML declaration's standalone attribute. Set to "yes" or "no".

  • version Indicates the value for the XML declaration's version attribute.

  • doctype-system and doctype-public let you specify an external DTD for XML documents. For example, <xsl:output doctype-system = "planets.dtd"/> produces <!DOCTYPE planets SYSTEM "planets.dtd"> .

That finishes our discussion of XSLT in this chapter. There's more that you can do with XSLT than we could cover in this chapter, such as sorting your output results using <xsl: sort > or creating new elements with <xsl:element> and so onif you're interested, track down a good XSLT book.

Категории