Here is my (simplified for this case 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.
</开发者_JS百科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 this:
<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 great, except for the xmlns="" in the segment element, that seems to be defining no namespace for itself and all of its children. How can I make it not add this?
Sidenote: I have also tried transforming the first node with
<xsl:template match="mydoc:doc">
<html xmlns="http://www/w3.org/1999/xhtml">
<xsl:apply-templates/>
</html>
</xsl:template>
instead, but it produces the same effect.
Thanks helpful people!
It seems like you want to put all elements in the output document into the "http://www/w3.org/1999/xhtml" namespace. Currently you only specify the namespace for the "newdoc" element, all other elements are in the default namespace since there is no namespace declaration in your stylesheet. The nesting inside the stylesheet determines to which namespace the elements belong, not the nesting after the transformation.
You can declare the default namespace in your stylesheet to affect all otherwise 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 also no longer need the xsl:element
tag and can directly use newdoc
to create a element in the correct namespace.
In the foo:div
template, you create a segment
element with the empty namespace. Since the parent element has a different namespace, the processor must add this namespace declaration.
If what you want is a segment
with the same namespace than the parent, then 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>
精彩评论