1

I need change namespace of the following xml

<?xml version="1.0" encoding="UTF-8"?>
<cli:IWResource xmlns:cli="namespace_1">
    <Name>MFCflowWithAdapter</Name>
    <Type>process</Type>
    <File>MFCflowWithAdapter.iwp</File>
    <Description/>
    <GUID>4C0BF41F-CB37-4815-B93F-1B6C2A103C6A</GUID>
    <targetServer>6.1.5</targetServer>
    <ImportedFrom>
        <ServerURI>http://maria-ubuntu:9000</ServerURI>
        <UserId>test</UserId>
        <Password>ENCR(3157318131043128321832252993249)</Password>
        <ImportDate>2012-07-26T16:22:52.644-04:00</ImportDate>
    </ImportedFrom>
    <Dependencies>
        <Dependency>
            <Name>RDBMS_oracle</Name>
            <Type>adapter</Type>
            <GUID>ACACF542E270035D7311E584E63F256C</GUID>
            <Path>/aa</Path>
            <UsageCount>1</UsageCount>
        </Dependency>
    </Dependencies>
</cli:IWResource>

To this:

<cli:IWResource xmlns:cli="namespace_2">

I tried various XSLT samples with some tweaks, but nothing seems to work.

Thanks!

3
  • Is there any reason you can't do a simple string replace on the source? Substituting the first occurrence of xmlns:cli="namespace_1" for xmlns:xli="namespace_2" is incredibly trivial to do in almost any language. Commented Aug 11, 2012 at 6:56
  • It always helps to show us your best attempt at a solution, because then we can tell what it is that you haven't understood. Commented Aug 11, 2012 at 8:32
  • @MichaelKay I agree, but I am just starting out with XSLT and have little knowledge of how to write transformations. This particular task is something I need to the project I am doing. Reading all the tutorials is a bit overwhelming for the time being due to little time I have. I should start learning XSLT as it is very useful. Commented Aug 13, 2012 at 13:50

2 Answers 2

1

This transformation:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:old="namespace_1" xmlns:new="namespace_2" >
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:variable name="vOldNS" select=
 "document('')/*/namespace::old"/>

 <xsl:variable name="vNewNS" select=
 "document('')/*/namespace::new"/>

 <xsl:template match="*">
  <xsl:choose>
   <xsl:when test="namespace-uri()=$vOldNS">
     <xsl:element name="{name()}" namespace="{$vNewNS}">
       <xsl:apply-templates select="@*|node()"/>
     </xsl:element>
   </xsl:when>
   <xsl:otherwise>
     <xsl:element name="{name()}" namespace="{namespace-uri()}">
       <xsl:apply-templates select="@*|node()"/>
     </xsl:element>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>

 <xsl:template match="@*">
  <xsl:copy-of select="."/>
 </xsl:template>
</xsl:stylesheet>

when applied on the provided XML document:

<cli:IWResource xmlns:cli="namespace_1">
    <Name>MFCflowWithAdapter</Name>
    <Type>process</Type>
    <File>MFCflowWithAdapter.iwp</File>
    <Description/>
    <GUID>4C0BF41F-CB37-4815-B93F-1B6C2A103C6A</GUID>
    <targetServer>6.1.5</targetServer>
    <ImportedFrom>
        <ServerURI>http://maria-ubuntu:9000</ServerURI>
        <UserId>test</UserId>
        <Password>ENCR(3157318131043128321832252993249)</Password>
        <ImportDate>2012-07-26T16:22:52.644-04:00</ImportDate>
    </ImportedFrom>
    <Dependencies>
        <Dependency>
            <Name>RDBMS_oracle</Name>
            <Type>adapter</Type>
            <GUID>ACACF542E270035D7311E584E63F256C</GUID>
            <Path>/aa</Path>
            <UsageCount>1</UsageCount>
        </Dependency>
    </Dependencies>
</cli:IWResource>

produces the wanted, correct result:

<cli:IWResource xmlns:cli="namespace_2">
   <Name>MFCflowWithAdapter</Name>
   <Type>process</Type>
   <File>MFCflowWithAdapter.iwp</File>
   <Description/>
   <GUID>4C0BF41F-CB37-4815-B93F-1B6C2A103C6A</GUID>
   <targetServer>6.1.5</targetServer>
   <ImportedFrom>
      <ServerURI>http://maria-ubuntu:9000</ServerURI>
      <UserId>test</UserId>
      <Password>ENCR(3157318131043128321832252993249)</Password>
      <ImportDate>2012-07-26T16:22:52.644-04:00</ImportDate>
   </ImportedFrom>
   <Dependencies>
      <Dependency>
         <Name>RDBMS_oracle</Name>
         <Type>adapter</Type>
         <GUID>ACACF542E270035D7311E584E63F256C</GUID>
         <Path>/aa</Path>
         <UsageCount>1</UsageCount>
      </Dependency>
   </Dependencies>
</cli:IWResource>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. I ran xslt (xsltcake.com/slices/61rlFL), but it doesn't seem to make the transformation. Am I doing something wrong?
@akravets, I always check my solutions and in fact copy and paste the results of the transformation into the answer. I would recommend that you run a normal XSLT 1.0 (or 2.0) processor and then you would get the results as in this answer.
1

This is specific for the root element:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:old="namespace_1"
xmlns:cli="namespace_2"
exclude-result-prefixes="old">

<xsl:template match="old:IWResource">
    <cli:IWResource>
        <xsl:apply-templates />
    </cli:IWResource>
</xsl:template>

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

</xsl:stylesheet>

Or do you need a general solution that works for different root elements and maybe even for children?

1 Comment

One needs to run this transformation to see that it doesn't produce the wanted result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.