Darshan4742
Darshan4742

Reputation: 56

How to remove namespaces from XML and add new namespaces?

I am receiving XML file with proper data. the receiver of the XML does not want few XML namespaces that I do not know why.

And few new namespaces should be added.

I have input file as

<Document xmlns="http://rep.evenex.dk/schema/evenex/eBusiness_01"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://rep.evenex.dk/schema/evenex/eBusiness_01 http://rep.evenex.dk/schema/evenex/eBusiness_01/e-commerce_101.xsd"
          System="HighJump"
          Version="101">
   <Header>
      <SenderEndpointID qualifier="EAN">98989898</SenderEndpointID>
      <ReceiverEndpointID qualifier="EAN">98989898</ReceiverEndpointID>
      <CreatedDate>13-06-2017</CreatedDate>
      <CreatedTime>10:18:00</CreatedTime>
      <EDIRefNo>6136</EDIRefNo>
      <Test>false</Test>
      <AcknowledgementRequest>No</AcknowledgementRequest>
   </Header>
   <Body>
      <DispatchReference>6136</DispatchReference>
      <DocumentType>ORDERS</DocumentType>
      <DocumentNo>98989898</DocumentNo>
      <DocumentDate>13-06-2017</DocumentDate>
      <RequestedDeliveryDate>19-06-2017</RequestedDeliveryDate>
      <CurrencyCode>DKK</CurrencyCode>
      <ExternalDocumentNo>100718360</ExternalDocumentNo>
      <ShipmentMethodCode>DAP</ShipmentMethodCode>
      <Parties>
         <Party type="Sellto">
            <No>98989898</No>
         </Party>
         <Party type="StoreNumber">
            <No>98989898</No>
         </Party>
         <Party type="Supplier">
            <No>98989898</No>
         </Party>
      </Parties>
      <Lines>
         <Line>
            <LineNo>1</LineNo>
            <DocumentNo>100718360</DocumentNo>
            <EANNo>98989898</EANNo>
            <Quantity>5</Quantity>
            <UnitofMeasure>PCE</UnitofMeasure>
            <UnitPrice>166.91</UnitPrice>
         </Line>
         <Line>
            <LineNo>2</LineNo>
            <DocumentNo>100718360</DocumentNo>
            <EANNo>98989898</EANNo>
            <Quantity>10</Quantity>
            <UnitofMeasure>PCE</UnitofMeasure>
            <UnitPrice>166.91</UnitPrice>
         </Line>
      </Lines>
   </Body>
</Document>

I need to remove few namespaces and add some new namespaces. Output should be

<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:xsd="http://www.w3.org/2001/XMLSchema"  SystemVersion="7.00.3.71.03"  System="HighJump"  Version="101">
  <Header>
    <SenderEndpointID qualifier="EAN">98989898</SenderEndpointID>
    <ReceiverEndpointID qualifier="EAN">98989898</ReceiverEndpointID>
    <CreatedDate>13-06-2017</CreatedDate>
    <CreatedTime>10:18:00</CreatedTime>
    <EDIRefNo>6136</EDIRefNo>
    <Test>false</Test>
    <AcknowledgementRequest>No</AcknowledgementRequest>
  </Header>
  <Body>
    <DispatchReference>6136</DispatchReference>
    <DocumentType>ORDERS</DocumentType>
    <DocumentNo>98989898</DocumentNo>
    <DocumentDate>13-06-2017</DocumentDate>
    <RequestedDeliveryDate>19-06-2017</RequestedDeliveryDate>
    <CurrencyCode>DKK</CurrencyCode>
    <ExternalDocumentNo>100718360</ExternalDocumentNo>
    <ShipmentMethodCode>DAP</ShipmentMethodCode>
    <Parties>
      <Party type="Sellto">
        <No>98989898</No>
      </Party>
      <Party type="StoreNumber">
        <No>98989898</No>
      </Party>
      <Party type="Supplier">
        <No>98989898</No>
      </Party>
    </Parties>
    <Lines>
      <Line>
        <LineNo>1</LineNo>
        <DocumentNo>100718360</DocumentNo>
        <EANNo>98989898</EANNo>
        <Quantity>5</Quantity>
        <UnitofMeasure>PCE</UnitofMeasure>
        <UnitPrice>166.91</UnitPrice>
      </Line>
      <Line>
        <LineNo>2</LineNo>
        <DocumentNo>100718360</DocumentNo>
        <EANNo>98989898</EANNo>
        <Quantity>10</Quantity>
        <UnitofMeasure>PCE</UnitofMeasure>
        <UnitPrice>166.91</UnitPrice>
      </Line>
    </Lines>
  </Body>
</Document>

I was trying with XSLT script to remove first and then add. But it's not working

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl    = "http://www.w3.org/1999/XSL/Transform"
    xmlns:xsi    = "http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd    = "http://www.w3.org/2001/XMLSchema"
    >

  <xsl:strip-space elements="*"/>
  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

  <!--Identity template,  provides default behavior that copies all content into the output -->
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

I want to remove following namespaces

xmlns="http://rep.evenex.dk/schema/evenex/eBusiness_01"
xsi:schemaLocation="http://rep.evenex.dk/schema/evenex/eBusiness_01 http://rep.evenex.dk/schema/evenex/eBusiness_01/e-commerce_101.xsd"

And add following namesapces

xmlns:xsd="http://www.w3.org/2001/XMLSchema"  
SystemVersion="7.00.3.71.03"

Upvotes: 0

Views: 1061

Answers (1)

Tim C
Tim C

Reputation: 70618

If you want to remove a namespace, what you are actually doing is creating new elements with the same local names, but not in any namespace. This template would do this:

<xsl:template match="*" priority="-0.4">
  <xsl:element name="{local-name()}">
    <xsl:apply-templates select="@*|node()"/>
  </xsl:element>
</xsl:template>    

The priority is to give it higher priority than the identity template (which has priority -0.5).

To xsi:schemaLocation, which is an attribute in a namespace, you can just have a simple template to ignore it

<xsl:template match="@xsi:schemaLocation" />

And to add the unnecessary namespace declaration for xsd, you would need to match the root element Document and add it there. You can also add the SystemVersion attribute at the same time.

<xsl:template match="/e:Document">
    <Document xmlns:xsd="http://www.w3.org/2001/XMLSchema" SystemVersion="7.00.3.71.03">
      <xsl:apply-templates select="@*|node()"/>
    </Document>
</xsl:template>

Note the use of a namespace prefix here, to ensure it matches a Document element that is in the namespace specified in the XML (The prefix e will be bound to the same namespace in the XSLT).

Try this XSLT

<xsl:stylesheet version="1.0"
    xmlns:xsl    = "http://www.w3.org/1999/XSL/Transform"
    xmlns:e      = "http://rep.evenex.dk/schema/evenex/eBusiness_01"
    xmlns:xsi    = "http://www.w3.org/2001/XMLSchema-instance"
    exclude-result-prefixes="e xsi">

  <xsl:strip-space elements="*"/>
  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

  <!--Identity template,  provides default behavior that copies all content into the output -->
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="*" priority="-0.4">
    <xsl:element name="{local-name()}">
      <xsl:apply-templates select="@*|node()"/>
    </xsl:element>
  </xsl:template>    

  <xsl:template match="/e:Document">
      <Document xmlns:xsd="http://www.w3.org/2001/XMLSchema" SystemVersion="7.00.3.71.03">
        <xsl:apply-templates select="@*|node()"/>
      </Document>
  </xsl:template>

  <xsl:template match="@xsi:schemaLocation" />
</xsl:stylesheet>

Upvotes: 2

Related Questions