Sometimes you will find that the result document contains namespace declarations that you don't want. You can use XSLT's exclude-result-prefixes attribute on the stylesheet element or xsl:exclude-result-prefixes on literal result elements to exclude such declarations from the result. This attribute contains a whitespace-separated list of one or more namespace prefixes that you want to exclude from the result tree, provided that the namespace is not actually used. In other words, you can use this attribute to keep superfluous namespaces defined in the stylesheet from reaching the result tree. Any namespace node found in the stylesheet gets copied to the result.
The stylesheet exclude.xsl shows you how to use this attribute: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:sc="http://www.wyeast.net/scand" xmlns:scand="http://www.wyeast.net/scandinavia" xmlns:nr="http://www.wyeast.net/scandinavia" exclude-result-prefixes="scand nr"> <xsl:output method="xml" indent="yes" encoding="ISO-8859-1"/> <xsl:template match="europe"> <xsl:apply-templates select="scandinavia"/> </xsl:template> <xsl:template match="scandinavia"> <sc:scandinavia> <xsl:apply-templates select="state"> <xsl:sort/> </xsl:apply-templates> </sc:scandinavia> </xsl:template> <xsl:template match="state"> <sc:country><xsl:value-of select="."/></sc:country> </xsl:template> </xsl:stylesheet> On the stylesheet element, two namespaces are declared, xmlns:scand="http://www.wyeast.net/scandinavia" and xmlns:nr="http://www.wyeast.net/scandinavia". They are desired in the stylesheet but are unnecessary in the result document. These namespaces are not written to the result tree because of the presence of the exclude-result-prefixes on stylesheet with scand and nr in its value. See this attribute in action by performing the transformation: xalan scand.xml exclude.xsl which gives this result: <?xml version="1.0" encoding="ISO-8859-1"?> <sc:scandinavia xmlns:sc="http://www.wyeast.net/scand"> <sc:country>Denmark</sc:country> <sc:country>Finland</sc:country> <sc:country>Iceland</sc:country> <sc:country>Norway</sc:country> <sc:country>Sweden</sc:country> </sc:scandinavia> You get only the needed namespace. Without the exclude-result-prefixes attribute, all the namespaces nodes on the stylesheet element would be included in the result tree. You can also use xsl:exclude-result-prefixes (when properly prefixed) on literal result elements. Example 14-3 shows an example, the stylesheet excludeonlit.xsl. Example 14-3. An XSLT stylesheet that excludes some prefixes<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:sc="http://www.wyeast.net/scand"> <xsl:output method="xml" indent="yes" encoding="ISO-8859-1"/> <xsl:template match="europe"> <xsl:apply-templates select="scandinavia"/> </xsl:template> <xsl:template match="scandinavia"> <sc:scandinavia xmlns:scand="http://www.wyeast.net/scandinavia" xmlns:nr="http://www.wyeast.net/scandinavia" xsl:exclude-result-prefixes="scand nr"> <xsl:apply-templates select="state"> <xsl:sort/> </xsl:apply-templates> </sc:scandinavia> </xsl:template> <xsl:template match="state"> <sc:country><xsl:value-of select="."/></sc:country> </xsl:template> </xsl:stylesheet> When used with scand.xml, you will get the same result with excludeonlit.xsl that you did with exclude.lit. |