xml XSLT:从元素中删除命名空间前缀

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9816252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 15:16:30  来源:igfitidea点击:

XSLT: Remove namespace prefix from elements

xmlxsltxslt-2.0

提问by Tony J

I need to remove the namespace prefix from an un-SOAP'd message.

我需要从非 SOAP 消息中删除命名空间前缀。

This is the message that has had the SOAP envelope removed. As you can see it contains ns1prefix on the elements:

这是删除了 SOAP 信封的消息。如您所见,它在元素上包含ns1前缀:

<ns1:BookingSource xmlns:ns1="urn:EDI/Booking/artifacts">
    <ns1:BookingHeader>
        <ns1:BookingNo>000123</ns1:BookingNo>
        <ns1:BookingDate>01/01/2012</ns1:BookingDate>
        <ns1:DSBookingDetail>
            <ns1:BookingNo>000123</ns1:BookingNo>
            <ns1:SeqNo>1</ns1:SeqNo>
            <ns1:LineType>Item</ns1:LineType>
            <ns1:ProductCode>Box</ns1:ProductCode>
        </ns1:DSBookingDetail>
        <ns1:DSBookingDetail>
            <ns1:BookingNo>000123</ns1:BookingNo>
            <ns1:SeqNo>2</ns1:SeqNo>
            <ns1:LineType>Item</ns1:LineType>
            <ns1:ProductCode>BrakeShoe</ns1:ProductCode>
        </ns1:DSBookingDetail>
    </ns1:DSBookingHeader>
    <ns1:BookingHeader>
        <ns1:BookingNo>000124</ns1:BookingNo>
        <ns1:BookingDate>01/01/2012</ns1:BookingDate>
        <ns1:DSBookingDetail>
            <ns1:BookingNo>000124</ns1:BookingNo>
            <ns1:SeqNo>1</ns1:SeqNo>
            <ns1:LineType>Item</ns1:LineType>
            <ns1:ProductCode>Box</ns1:ProductCode>
        </ns1:DSBookingDetail>
        <ns1:DSBookingDetail>
            <ns1:BookingNo>000124</ns1:BookingNo>
            <ns1:SeqNo>2</ns1:SeqNo>
            <ns1:LineType>Item</ns1:LineType>
            <ns1:ProductCode>BrakeShoe</ns1:ProductCode>
        </ns1:DSBookingDetail>
    </ns1:DSBookingHeader>
</ns1:BookingSource>

To this:

对此:

<BookingSource>
    <BookingHeader>
        <BookingNo>000123</BookingNo>
        <BookingDate>01/01/2012</BookingDate>
        <DSBookingDetail>
            <BookingNo>000123</BookingNo>
            <SeqNo>1</SeqNo>
            <LineType>Item</LineType>
            <ProductCode>Box</ProductCode>
        </DSBookingDetail>
        <DSBookingDetail>
            <BookingNo>000123</BookingNo>
            <SeqNo>2</SeqNo>
            <LineType>Item</LineType>
            <ProductCode>BrakeShoe</ProductCode>
        </DSBookingDetail>
    </DSBookingHeader>
    <BookingHeader>
        <BookingNo>000124</BookingNo>
        <BookingDate>01/01/2012</BookingDate>
        <DSBookingDetail>
            <BookingNo>000124</BookingNo>
            <SeqNo>1</SeqNo>
            <LineType>Item</LineType>
            <ProductCode>Box</ProductCode>
        </DSBookingDetail>
        <DSBookingDetail>
            <BookingNo>000124</BookingNo>
            <SeqNo>2</ns1:SeqNo>
            <LineType>Item</LineType>
            <ProductCode>BrakeShoe</ProductCode>
        </DSBookingDetail>
    </DSBookingHeader>
</BookingSource>

I've searched through the KB and found some hints on how to do it, but the final solution evades me.

我搜索了知识库并找到了一些有关如何执行此操作的提示,但最终的解决方案回避了我。

Thanks, Tony.

谢谢,托尼。

回答by InfantPro'Aravind'

It is called namespace, below is a code to remove namespace from all elements and attributes ..

它被称为命名空间,下面是从所有元素和属性中删除命名空间的代码..

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

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