如何防止XSLT样式表中的这些冗余名称空间? [英] How can I prevent these redundant namespaces from an XSLT stylesheet?

查看:62
本文介绍了如何防止XSLT样式表中的这些冗余名称空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用XSLT样式表将包含嵌入式XHTML(使用名称空间)的XML文件转换为纯XHTML时,我在最初是XHTML的元素上留有多余的名称空间定义.简单的测试用例:

When using an XSLT stylesheet to transform an XML file which contains embedded XHTML (using namespaces) into pure XHTML, I'm left with redundant namespace definitions on the elements which were originally XHTML. Simple test case:

XML:

<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xml" href="fbb.xsl"?>
<foo xmlns="urn:foo:bar:baz" xmlns:html="http://www.w3.org/1999/xhtml">
    <bar>
        <baz>Some <html:i>example</html:i> text.</baz>
    </bar>
</foo>

XSL:

<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0" xmlns="http://www.w3.org/1999/xhtml" xmlns:fbb="urn:foo:bar:baz" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="fbb">
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="/fbb:foo">
        <html>
            <head>
                <title>Example</title>
            </head>

            <body>
                <p>
                    <xsl:copy-of select="fbb:bar/fbb:baz/node()"/>
                </p>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

输出:

<?xml version="1.0"?>
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Example</title>
  </head>
  <body>
    <p>Some <html:i xmlns="urn:foo:bar:baz" xmlns:html="http://www.w3.org/1999/xhtml">example</html:i> text.</p>
  </body>
</html>

是否可以防止将冗余名称空间(和前缀)添加到<i>元素? (作为参考,我在Cygwin上将xsltproclibxml2-2.7.3libxslt-1.1.24一起使用.)

Is it possible to prevent the redundant namespaces (and prefix) from being added to the <i> element? (For reference, I'm using xsltproc with libxml2-2.7.3 and libxslt-1.1.24 on Cygwin.)

推荐答案

而不是xsl:copy-of使用身份转换模板并从XHTML元素中删除名称空间前缀.

Instead of xsl:copy-of use an identity transformation templates and remove the namespace prefix from XHTML elements.

<xsl:stylesheet version="1.0"
                xmlns="http://www.w3.org/1999/xhtml"
                xmlns:fbb="urn:foo:bar:baz"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:html="http://www.w3.org/1999/xhtml"
                exclude-result-prefixes="fbb html">

  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/fbb:foo">
    <html>
      <head>
        <title>Example</title>
      </head>
      <body>
        <p>
          <xsl:apply-templates select="fbb:bar/fbb:baz/node()"/>
        </p>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="html:*">
    <xsl:element name="{local-name()}">
      <xsl:apply-templates select="@* | node()"/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="node() | @*">
    <xsl:copy>
      <xsl:apply-templates select="node() | @*"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

这篇关于如何防止XSLT样式表中的这些冗余名称空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆