【问题标题】:XML without namespace没有命名空间的 XML
【发布时间】:2012-12-31 06:52:36
【问题描述】:

我需要从信封中提取 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>

请帮忙。

【问题讨论】:

  • 这是剥离标签,而不仅仅是“删除命名空间”。
  • 就是这样。我需要将此提取的 XML 作为另一个 XML 的一部分包含在内。这就是为什么我不能拥有命名空间。

标签: xslt namespaces transform xml-namespaces


【解决方案1】:

这应该适用于剥离命名空间:

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

但我有点怀疑您是否需要这样做。如果您要附加 this 的 XML 使用相同的命名空间,并且 this 是 this 包含的 XML 应该具有的命名空间,则该 XML 中是否有命名空间声明应该无关紧要。

【讨论】:

  • 他们有不同的命名空间。这就是为什么我不能保留这个。
猜你喜欢
  • 1970-01-01
  • 2018-12-07
  • 1970-01-01
  • 2013-02-18
  • 2014-03-23
  • 1970-01-01
  • 2013-10-26
  • 2015-09-05
  • 2019-02-15
相关资源
最近更新 更多