加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何摆脱XSLT输出中的xmlns =“”(无命名空间)属性

发布时间:2020-12-16 07:47:07 所属栏目:百科 来源:网络整理
导读:这是我的(针对这种情况简化) XML: ?xml version="1.0" encoding="UTF-8"??xml-stylesheet href="test_1.xsl" type="text/xsl"?doc xmlns="http://www.foo.org" div titleMr. Title/title paragraphThis is one paragraph. /paragraph paragraphAnother para
这是我的(针对这种情况简化) 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>

相反,但它产生相同的效果.

谢谢有帮助的人!

您似乎希望将输出文档中的所有元素放入“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在正确的命名空间中创建元素.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读