Unnamed user
Unnamed user

Reputation: 181

XSLT to change namespace

I have this XML code:

    <soapenv:Envelope xmlns:soapenv="something2" xmlns:oas="something1" xmlns:bill="something">
    <soapenv:Header>
    <wsse:Security xmlns:wsse="something_else">
     <wsse:UsernameToken>
        <wsse:Username>SYSUSER</wsse:Username>
        <wsse:Password Type="PasswordText">sysuser00</wsse:Password>
     </wsse:UsernameToken>
    </wsse:Security>
   </soapenv:Header>
    <soapenv:Body>
   <GetBillList xmlns="old_uri" xmlns:xsi="else" xsi:schemaLocation="old_schema">
   <Case>
   <CaseID>699677</CaseID> 
   <BillGroup casref="123">
   <BillGroupID>1</BillGroupID> 
   </BillGroup>
   </Case>
   <StartDate>2014-06-12</StartDate> 
   </GetBillList>
   </soapenv:Body>
   </soapenv:Envelope>

I want to change old_uri & old_schema into new_uri & new_schema respectively, along with attribute conversion into element of BillGroup node.

XSLT used:

   <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:bill="something">
   <xsl:output method="xml" indent="yes"/>
   <xsl:template match="@*|node()">
   <xsl:copy>
   <xsl:apply-templates select="@*|node()"/>
   </xsl:copy>
   </xsl:template>
   <xsl:template match="bill:BillGroup">
   <xsl:element name="{local-name()}">
   <xsl:value-of select="."/>
   </xsl:element>
   </xsl:template>
   </xsl:stylesheet>

Target XML:

   <soapenv:Envelope xmlns:soapenv="something2" xmlns:oas="something1" xmlns:bill="something">
    <soapenv:Header>
    <wsse:Security xmlns:wsse="something_else">
        <wsse:UsernameToken>
            <wsse:Username>SYSUSER</wsse:Username>
            <wsse:Password Type="PasswordText">sysuser00</wsse:Password>
        </wsse:UsernameToken>
    </wsse:Security>
   </soapenv:Header>
    <soapenv:Body>
    <GetBillList xmlns="new_uri" xmlns:xsi="else" xsi:schemaLocation="new_schema">
        <Case>
            <CaseID>699677</CaseID>
            <BillGroup>
                <casref>123</casref>
                <BillGroupID>1</BillGroupID>
            </BillGroup>
        </Case>
        <StartDate>2014-06-12</StartDate>
       </GetBillList>
        </soapenv:Body>
     </soapenv:Envelope>

Would appreciate any help on this.

Upvotes: 1

Views: 1665

Answers (1)

Tim C
Tim C

Reputation: 70648

In your input XML, your billGroup element is part of the old_uri namespace, not the something namespace. This means you need to adjust your namespace prefixes accordingly. You also need to change it to match the attribute of billGroup if you want to change the attribute into an element:

<xsl:template match="old:BillGroup/@*">
   <xsl:element name="{local-name()}" namespace="new_uri">
      <xsl:value-of select="."/>
   </xsl:element>
</xsl:template>

Here old is a prefix that is bound to the uri old_uri.

To change the namespace uri from old_uri to new_uri you can have a template that matches elements in the old namespace

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

And to change the schema location, have a template that matches the attribute

<xsl:template match="@xsi:schemaLocation">
   <xsl:attribute name="{name()}">
      <xsl:text>new_schema</xsl:text>
   </xsl:attribute>
</xsl:template>

Try this XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="else" xmlns:old="old_uri" >
<xsl:output method="xml" indent="yes"/>

<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="@xsi:schemaLocation">
  <xsl:attribute name="{name()}">
    <xsl:text>new_schema</xsl:text>
  </xsl:attribute>
</xsl:template>

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

<xsl:template match="old:BillGroup/@*">
  <xsl:element name="{local-name()}" namespace="new_uri">
    <xsl:value-of select="."/>
  </xsl:element>
</xsl:template>
</xsl:stylesheet>

Upvotes: 4

Related Questions