使用XSLT删除元素标记中的xmlns

时间:2014-07-15 05:21:42

标签: xslt

任何人都可以帮我解决如何删除元素标记中的xmlns的问题吗?

XML输入:

<gl-cor:entityInformation>
<gl-cor:entityPhoneNumber xmlns:gl-bus="http://www.xbrl.org/int/gl/bus/2010-04-12">
    <gl-cor:phoneNumberDescription contextRef="journal_context">main</gl-cor:phoneNumberDescription>
    <gl-cor:phoneNumber contextRef="journal_context">012-34-56</gl-cor:phoneNumber>
</gl-cor:entityPhoneNumber>
<gl-cor:entityFaxNumberStructure xmlns:gl-bus="http://www.xbrl.org/int/gl/bus/2010-04-12">
    <gl-cor:entityFaxNumber contextRef="journal_context">012-34-56</gl-cor:entityFaxNumber>
</gl-cor:entityFaxNumberStructure>
<gl-cor:entityEmailAddressStructure xmlns:gl-bus="http://www.xbrl.org/int/gl/bus/2010-04-12">
    <gl-cor:entityEmailAddress contextRef="journal_context">ma.johanna.estrivo@infor.com</gl-cor:entityEmailAddress>
</gl-cor:entityEmailAddressStructure>

我将如何删除xmlns:gl-bus =&#34; http://www.xbrl.org/int/gl/bus/2010-04-12"?

预期输出应为:

<gl-cor:entityPhoneNumber>
<gl-cor:phoneNumberDescription contextRef="journal_context">main</gl-cor:phoneNumberDescription>
<gl-cor:phoneNumber contextRef="journal_context">012-34-56</gl-cor:phoneNumber>

    012-34-56     ma.johanna.estrivo@infor.com

我在这个代码中使用XSLT:

  <xsl:template match="*[ancestor::gl-cor:entityInformation]">
  <!-- change element prefix -->
  <xsl:element name="gl-bus:{local-name()}">
      <!-- process attributes -->
      <xsl:for-each select="@*">
          <!-- remove attribute prefix -->
          <xsl:attribute name="{local-name()}">
              <xsl:value-of select="."/>
          </xsl:attribute>
      </xsl:for-each>
  <xsl:apply-templates/>
</xsl:element>

1 个答案:

答案 0 :(得分:0)

首先,如果不指定此前缀的命名空间,则无法输出前缀为gl-bus:的元素!

xmlns:gl-bus必须位于文档中的某个位置 - 可能位于根元素处。

此解决方案在此处创建默认命名空间 - 并删除前缀(它取决于您的其他人XSLT):

<entityInformation xmlns="http://www.xbrl.org/int/gl/bus/2010-04-12">
    <entityPhoneNumber>
        <phoneNumberDescription contextRef="journal_context">main</phoneNumberDescription>
        <phoneNumber contextRef="journal_context">012-34-56</phoneNumber>
    </entityPhoneNumber>
    <entityFaxNumberStructure>
        <entityFaxNumber contextRef="journal_context">012-34-56</entityFaxNumber>
    </entityFaxNumberStructure>
    <entityEmailAddressStructure>
        <entityEmailAddress contextRef="journal_context">ma.johanna.estrivo@infor.com</entityEmailAddress>
    </entityEmailAddressStructure>
</entityInformation>

使用这个XSLT:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema"  xmlns:gl-bus="http://www.xbrl.org/int/gl/bus/2010-04-12"  xmlns:gl-cor="www.any.com" version="2.0">
    <xsl:output method="xml" encoding="UTF-8" indent="yes"/>

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

    <xsl:template match="*">
        <!-- change element prefix -->
        <xsl:element name="{local-name()}" namespace="http://www.xbrl.org/int/gl/bus/2010-04-12">
            <!-- process attributes -->
            <xsl:for-each select="@*">
                <!-- remove attribute prefix -->
                <xsl:attribute name="{local-name()}">
                    <xsl:value-of select="."/>
                </xsl:attribute>
            </xsl:for-each>
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>

如果要删除所有名称空间,只需另一种可能的解决方案,只输出local-name()

此XSLT将不输出名称空间:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema"  xmlns:gl-bus="http://www.xbrl.org/int/gl/bus/2010-04-12"  xmlns:gl-cor="www.any.com" version="2.0">
    <xsl:output method="xml" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/>

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

    <xsl:template match="*">
        <!-- change element prefix -->
        <xsl:element name="{local-name()}">
            <!-- process attributes -->
            <xsl:for-each select="@*">
                <!-- remove attribute prefix -->
                <xsl:attribute name="{local-name()}">
                    <xsl:value-of select="."/>
                </xsl:attribute>
            </xsl:for-each>
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>

提供以下输出:

<entityInformation>
    <entityPhoneNumber>
        <phoneNumberDescription contextRef="journal_context">main</phoneNumberDescription>
        <phoneNumber contextRef="journal_context">012-34-56</phoneNumber>
    </entityPhoneNumber>
    <entityFaxNumberStructure>
        <entityFaxNumber contextRef="journal_context">012-34-56</entityFaxNumber>
    </entityFaxNumberStructure>
    <entityEmailAddressStructure>
        <entityEmailAddress contextRef="journal_context">ma.johanna.estrivo@infor.com</entityEmailAddress>
    </entityEmailAddressStructure>
</entityInformation>