【问题标题】:How to get rid of xmlns="" (no-namespace) attribute in XSLT output如何摆脱 XSLT 输出中的 xmlns="" (no-namespace) 属性
【发布时间】:2012-09-26 15:04:57
【问题描述】:

这是我的(为本案例场景简化的)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>

这是我的 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>

输出产生这个:

<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>

这很好,除了段元素中的 xmlns="" 之外,它似乎没有为它自己及其所有子元素定义命名空间。我怎样才能让它不添加这个?

旁注:我也尝试过用

转换第一个节点
<xsl:template match="mydoc:doc">
  <html xmlns="http://www/w3.org/1999/xhtml">
   <xsl:apply-templates/>
  </html>
 </xsl:template>

相反,但它会产生相同的效果。

感谢乐于助人的人!

【问题讨论】:

    标签: xml xslt


    【解决方案1】:

    您似乎想将输出文档中的所有元素放入“http://www/w3.org/1999/xhtml”命名空间。目前您只为“newdoc”元素指定命名空间,所有其他元素都在默认命名空间中,因为您的样式表中没有命名空间声明。样式表内的嵌套决定了元素属于哪个命名空间,而不是转换后的嵌套。

    您可以在样式表中声明默认命名空间以影响所有其他不合格的元素:

    <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">
    

    现在您也不再需要xsl:element 标签,可以直接使用newdoc 在正确的命名空间中创建元素。

    【讨论】:

    • örn:谢谢!工作就像一个魅力,我想我现在明白了。
    • 这对我也有用,但答案中有错字。答案是http://www/w3.org ... 应该是http://www.w3.org ...。斜线应该是一个点。
    【解决方案2】:

    foo:div 模板中,您创建一个带有空命名空间的segment 元素。由于父元素有不同的命名空间,处理器必须添加这个命名空间声明。

    如果您想要的是一个与父级具有相同命名空间的segment,那么请改用xsl:element

    <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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多