从XML中排除名称空间

时间:2012-03-28 09:05:25

标签: xslt transform transformation

我的xsl文件:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:go="http://www.google.com"
    exclude-result-prefixes="go">
<xsl:include href="SomeLibrary.xsl"/>

    <xsl:template match="/">

        <xsl:call-template name="SomeTemplate">
            <xsl:with-param name="Element" select="'randomParam'"/>
        </xsl:call-template>

    </xsl:template>

</xsl:stylesheet>

SomeLibrary.xsl文件:

<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.google.com">
    <xsl:template name="SomeTemplate">
    <xsl:param name="Element"/>
        <Blabla>
            <xsl:value-of select="$Element" />    
        </Blabla>
    </xsl:template>
</xsl:stylesheet>

输入xml:只使用一个空XML。结果如下:

<?xml version="1.0" encoding="UTF-8"?>

<Blabla xmlns="http://www.google.com">
    randomParam
</Blabla>

我想要的是拥有&#34; Blabla&#34;没有命名空间的节点。如何在不修改我的&#34; SomeLibrary.xsl&#34;?

的情况下将其删除或确保它无法到达目的地

3 个答案:

答案 0 :(得分:2)

如果您不想编辑导入的样式表代码,删除命名空间的方法是使用两遍转换(在XSLT 1.0中需要使用xxx:node-set()扩展函数):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:ext="http://exslt.org/common" exclude-result-prefixes="ext" >
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">
   <xsl:variable name="vrtfPass1">
     <xsl:call-template name="SomeTemplate">
                <xsl:with-param name="Element" select="'randomParam'"/>
       </xsl:call-template>
     </xsl:variable>

     <xsl:variable name="vPass1" select="ext:node-set($vrtfPass1)"/>
     <xsl:apply-templates select="$vrtfPass1/node()" mode="pass2"/>
 </xsl:template>

 <xsl:template match="*" mode="pass2">
  <xsl:element name="{name()}">
   <xsl:copy-of select="@*"/>
   <xsl:apply-templates mode="pass2"/>
  </xsl:element>
 </xsl:template>

 <xsl:template name="SomeTemplate" xmlns="http://www.google.com">
   <xsl:param name="Element"/>
            <Blabla>
                <xsl:value-of select="$Element" />
            </Blabla>
 </xsl:template>
</xsl:stylesheet>

当对任何XML文档(未使用)应用此转换时,会生成所需结果(已删除默认命名空间)

<Blabla>randomParam</Blabla>

<强>更新

OP已在评论中指出他正在使用Xalan 2.07。

下面几乎是相同的解决方案,但是使用了xxx:node-set()函数的名称空间和名称,如Xalan所用:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:x="xmlns:xalan="http://xml.apache.org/xalan" exclude-result-prefixes="x" >
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">
   <xsl:variable name="vrtfPass1">
     <xsl:call-template name="SomeTemplate">
                <xsl:with-param name="Element" select="'randomParam'"/>
       </xsl:call-template>
     </xsl:variable>

     <xsl:variable name="vPass1" select="x:nodeset($vrtfPass1)"/>
     <xsl:apply-templates select="$vrtfPass1/node()" mode="pass2"/>
 </xsl:template>

 <xsl:template match="*" mode="pass2">
  <xsl:element name="{name()}">
   <xsl:copy-of select="@*"/>
   <xsl:apply-templates mode="pass2"/>
  </xsl:element>
 </xsl:template>

 <xsl:template name="SomeTemplate" xmlns="http://www.google.com">
   <xsl:param name="Element"/>
            <Blabla>
                <xsl:value-of select="$Element" />
            </Blabla>
 </xsl:template>
</xsl:stylesheet>

答案 1 :(得分:1)

使用以下命令显式创建它们:

<xsl:element name="Blabla" namespace="">...</xsl:element>

但这意味着摆弄SomeLibrary.xsl文件。

答案 2 :(得分:0)

exclude-result-prefixes="go"添加到样式表元素中 当然,将名称空间声明从默认声明更新为xmlns:go="http://www.google.com"