Prevent Namespace Prefixes from Being Copied to the Output
Written by
Gregory Scot Collins
Tuesday, 2 May 2006, 10:02 AM
This article has been tested to work with the following products and versions. No guarantee of compatibility, with or without modification, is offered for products or versions other than those listed.
- JavaScript 1.7 (Netscape Communications)
- HTML 4.01 (W3C)
- XML 1.0 (W3C)
- XPath 1.0 (W3C)
- XSLT 1.0 (W3C)
IN THIS ARTICLE:
Any namespace prefix specified on the xsl:stylesheet element is automatically copied to your output. If, for example, your XSLT employs JavaScript code, the specified namespace prefix required to call the JavaScript functions will be copied to your output. Let's create some test files to demonstrate the issue and the solution.
Create the source files
We need a simple XML file to transform. Copy the contents of Listing 1 into a text editor, and then save the file as Test.xml.
Copy the contents of Listing 2 into a text editor, and then save the file as Test.xsl. Be sure to save it in the same folder the as Test.xml file.
Applying the transform to our XML results in the following output:
<html xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:js="JavaScript">
<head><title>BRAINTROVE</title></head><body>visit http://www.braintrove.com</body>
</html>
In many situations the additional namespace prefixes will not pose a problem; however, you might prefer to have them not present. This is easily fixed.
Update the XSLT to exclude specific namespace prefixes from the output
You can prevent specific namespace prefixes from being copied to your output by using the optional exclude-result-prefixes attribute on the xsl:stylesheet element. Use a whitespace separator between each prefix you want excluded from your output.
Listing 3 shows the addition of the exclude-result-prefixes attribute, highlighted in green:
Applying the updated transform to our XML results in the following output that correctly excludes the additional namespace prefixes:
<html>
<head><title>BRAINTROVE</title></head><body>visit http://www.braintrove.com</body>
</html>
Copyright ©2006
Braintrove. All rights reserved. This material may not be copied, published, broadcast, rewritten
or redistributed. You may, however, use the techniques, along with any associated code and files in
your development. This material is provided "AS IS" without warranty of any kind. You accept full
responsibility and liability for any and all results associated with use of this material.


Stumble It!
Digg It!
del.icio.us




