Omitting the XML Declaration and Generating XML Fragments

Many people new to XSLT find themselves frustrated by the XML declaration that always seems to appear at the tops of their result documents, especially if they don’t realize that the default output method is XML. Of course, there’s a way of getting rid of the XML declaration, and it’s useful if you’re creating well-formed XML fragments that are not necessarily complete documents. You just have to set the omit-xml-declaration attribute of the <xsl:output> element to “yes”: <xsl:output method="xml" omit-xml-declaration="yes"/>.

In the following example, I just strip the XML declaration from a document, copying everything else. Note that the XML declaration is not a node, so there’s no danger ...

Get Inside XSLT now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.