XML没有命名空间

4

我需要从一个信封中提取一个XML。但我无法得到我预期的输出。我需要在输出中去掉命名空间。

我的输入:

<ns1:input xmlns:ns1="http://mysapmle.org/" xmlns="http://othersample.org/">
        <sample>
            <inner tc="5">Test</inner>
            <routine>Always</routine>
        </sample>
</ns1:input>

我的期望输出:

<sample>
    <inner tc="5">Test</inner>
    <routine>Always</routine>
</sample>

我的实际输出:

    <sample xmlns="http://othersample.org/">
            <inner tc="5">Test</inner>
            <routine>Always</routine>
        </sample>

我的XSLT:
<xsl:output omit-xml-declaration="yes" indent="yes" />

<xsl:template match="/">
    <xsl:copy copy-namespaces="no">
        <xsl:apply-templates select="//sample" />           
    </xsl:copy>     
</xsl:template> 

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

请帮忙。


1
这是剥离标签,而不仅仅是“删除命名空间”。 - Matt Ball
1
这有点棘手。我需要将提取的XML包含在另一个XML中。这就是为什么我不能有命名空间的原因。 - user1985027
1个回答

2

以下方法可用于去除命名空间:

<xsl:output omit-xml-declaration="yes" indent="yes" />

<xsl:template match="/">
   <xsl:apply-templates select="*/*" />
</xsl:template>

<xsl:template match="@*">
    <xsl:attribute name="{local-name()}">
        <xsl:value-of select="."/>          
    </xsl:attribute>     
</xsl:template>

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

但我有点怀疑你是否需要这样做。如果你要添加的XML使用相同的命名空间,并且这个包含XML应该具有该命名空间,则其中包含的XML具有命名空间声明并不重要。


他们有不同的命名空间。这就是为什么我不能保留这个的原因。 - user1985027

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接