Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

xml - XSLT: Remove namespace prefix from elements

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

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...