5

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.

Rookie Programmer Aravind
  • 11,952
  • 23
  • 81
  • 114
Tony J
  • 107
  • 1
  • 3
  • 8

1 Answers1

21

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>
Rookie Programmer Aravind
  • 11,952
  • 23
  • 81
  • 114
  • 1
    No experienced XML person would describe this transformation as "removing the prefixes". You are doing something much more fundamental - you are renaming the elements by changing their namespace. – Michael Kay Mar 22 '12 at 08:47
  • 4
    @MichaelKay, You are commenting in wrong place. I didn't call it "removing prefix", the op did, instead I corrected. I think you are suppose to comment on question. – Rookie Programmer Aravind Mar 22 '12 at 10:47