How to get rid of xmlns = "" (no-namespace) attribute in XSLT output

Here is my (simplified for this scenario) XML:

<?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet href="test_1.xsl" type="text/xsl"?> <doc xmlns="http://www.foo.org"> <div> <title>Mr. Title</title> <paragraph>This is one paragraph. </paragraph> <paragraph>Another paragraph. </paragraph> </div> </doc> 

And here is my XSLT:

 <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:foo="http://www.foo.org"> <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="foo:doc"> <xsl:element name="newdoc" namespace="http://www/w3.org/1999/xhtml"> <xsl:apply-templates/> </xsl:element> </xsl:template> <xsl:template match="foo:div"> <segment title="{foo:title}"> <xsl:apply-templates/> </segment> </xsl:template> <xsl:template match="foo:title"> <xsl:element name="h2"> <xsl:apply-templates/> </xsl:element> </xsl:template> <xsl:template match="foo:paragraph"> <xsl:element name="p"> <xsl:apply-templates/> </xsl:element> </xsl:template> </xsl:stylesheet> 

The output produces the following:

 <newdoc xmlns="http://www/w3.org/1999/xhtml"> <segment xmlns="" title="Mr. Title"> <h2>Mr. Title</h2> <p>This is one paragraph. </p> <p>Another paragraph. </p> </segment> </newdoc> 

which is fine, except for xmlns = "" in a segment element, which does not appear to define a namespace for itself and all its children. How can I make it not add this?

Sidenote: I also tried converting the first node with

 <xsl:template match="mydoc:doc"> <html xmlns="http://www/w3.org/1999/xhtml"> <xsl:apply-templates/> </html> </xsl:template> 

but it produces the same effect.

Thanks, helpful people!

+7
source share
2 answers

It looks like you want to put all the elements in the output document into the http: //www/w3.org/1999/xhtml namespace. Currently, you only specify a namespace for the "newdoc" element, all other elements are in the default namespace because there is no namespace declaration in your stylesheet. Nesting within a stylesheet determines which namespace the elements belong to, and not nesting after conversion.

You can declare a default namespace in your stylesheet to affect all other unqualified elements:

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:foo="http://www.foo.org" xmlns="http://www.w3.org/1999/xhtml"> 

Now you no longer need the xsl:element tag and it can directly use newdoc to create the element in the correct namespace.

+11
source

In the foo:div template, you create a segment element with an empty namespace. Because the parent has a different namespace, the processor must add this namespace declaration.

If you want, this is a segment with the same namespace as the parent, use xsl:element instead:

 <xsl:template match="foo:div"> <xsl:element name="segment"> <xsl:attribute name="title"> <xsl:value-of select="foo:title"/> </xsl:attribute> <xsl:apply-templates/> </xsl:element> </xsl:template> 
+4
source

All Articles