1

Sample XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns:xslnsv="http://sample2.1">
<xsl:output method="xml" indent="yes"/>

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

<xsl:template match="//xslnsv:Activity">
  <xsl:copy>
    <xsl:copy-of select="@*" />
    <xsl:if test="not(@IsForCompensation) 
      and (./xslnsv:IsForCompensationSpecified)">
      <xsl:attribute name="IsForCompensation">
        <xsl:value-of 
          select="./xslnsv:IsForCompensationSpecified" />
      </xsl:attribute>
    </xsl:if>
    <xsl:apply-templates
      select="@*|node()[local-name() 
        != 'IsForCompensationSpecified']" />
  </xsl:copy>
</xsl:template>    
</xsl:stylesheet>

Here we have a namespace xmlns:xslnsv="http://sample2.2" It work when we have a xml with same namespace

<?xml version="1.0" encoding="utf-8"?>
<Package xmlns="http://sample2.2" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:xsd="http://www.w3.org/2001/XMLSchema" >
  <ElementAtLevel1>
    <ElementAtLevel2 Id="cf9d2" Name="Pool 1">    
      <Activities>
        <Activity Id="ef84125a">          
          <IsForCompensationSpecified
            >false</IsForCompensationSpecified>
        </Activity>
        <Activity Id="39c5b8d8" Name="Task 1">
          <IsForCompensationSpecified 
            >true</IsForCompensationSpecified>
        </Activity>
      </Activities>
    </ElementAtLevel2>  
  </ElementAtLevel1>
  <ExtendedAttributes />
</Package>

to produce output as:

<?xml version="1.0"?>
<Package xmlns="http://sample2.2" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     
  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ElementAtLevel1>
    <ElementAtLevel2 Id="cf9d267d-e1ed-4616-adfb-d24d6844f775"
                     Name="Pool 1">    
      <Activities>
        <Activity Id="ef84125a-0a01-4d76-9b3b-413ffb3c7a74"    
                  IsForCompensation="false"/>
        <Activity Id="39c5b8d8-9a72-40d1-b3e4-8cd973ccdf03" 
                  Name="Task 1" 
                  IsForCompensation="true"/>
      </Activities>
    </ElementAtLevel2>  
  </ElementAtLevel1>
  <ExtendedAttributes/>
</Package>

But the problem is: We have some of xmls with different namespace ie http://sample2.1 Sample xml with different namespace

<?xml version="1.0"?>
<Package xmlns="http://sample2.1" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     
  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ElementAtLevel1>
    <ElementAtLevel2 Id="cf9d267d-e1ed-4616-adfb-d24d6844f775" 
                     Name="Pool 1">    
      <Activities>
        <Activity Id="ef84125a-0a01-4d76-9b3b-413ffb3c7a74" 
                  IsForCompensation="false"/>
        <Activity Id="39c5b8d8-9a72-40d1-b3e4-8cd973ccdf03" 
                  Name="Task 1" 
                  IsForCompensation="true"/>
      </Activities>
    </ElementAtLevel2>  
  </ElementAtLevel1>
  <ExtendedAttributes/>
</Package>

then we don't have right output.

<?xml version="1.0" encoding="utf-8"?>
<Package xmlns="http://sample2.1" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:xsd="http://www.w3.org/2001/XMLSchema" >
  <ElementAtLevel1>
    <ElementAtLevel2 Id="cf9d2" Name="Pool 1">    
      <Activities>
        <Activity Id="ef84125a">
          <IsForCompensationSpecified
            >false</IsForCompensationSpecified>
        </Activity>
        <Activity Id="39c5b8d8" Name="Task 1">
          <IsForCompensationSpecified
            >true</IsForCompensationSpecified>
        </Activity>
      </Activities>
    </ElementAtLevel2>  
  </ElementAtLevel1>
  <ExtendedAttributes />
</Package>

I modified the xslt to change the namespace on the fly. sample xslt with new changes

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns:xslnsv="http://sample2.2" >

  <xsl:output method="xml" indent="yes"/>
  <xsl:variable name="vUrl" select="'http://sample2.2'"/>

  <xsl:template match="*[namespace-uri()='http://sample2.1']">
    <xsl:element name="{name()}" namespace="{$vUrl}">
      <xsl:copy-of select="@*"/>
       <xsl:apply-templates/>
    </xsl:element>
  </xsl:template> 
  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>
 <xsl:template match="//xslnsv:Activity">
   <xsl:copy>
     <xsl:copy-of select="@*" />
     <xsl:if test="not(@IsForCompensation) 
                   and (./xslnsv:IsForCompensationSpecified)">
       <xsl:attribute name="IsForCompensation">
         <xsl:value-of 
           select="./xslnsv:IsForCompensationSpecified" />
       </xsl:attribute>
     </xsl:if>
     <xsl:apply-templates select="@*
       |node()[local-name() != 'IsForCompensationSpecified']" />
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

It looks to me, it is able to change the namespace, but not able to pick the element after changing the namespace. may be it is refering to old namespace ie 2.1 from source xml

but still i am not getting the right output; i am getting following output.

<?xml version="1.0"?>
 <Package xmlns="http://sample2.1" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <ElementAtLevel1>
 <ElementAtLevel2 Id="cf9d2" Name="Pool 1">    
 <Activities>
 <Activity Id="ef84125a">          
 <IsForCompensationSpecified>false</IsForCompensationSpecified>
 </Activity>
 <Activity Id="39c5b8d8" Name="Task 1">
 <IsForCompensationSpecified>true</IsForCompensationSpecified>
 </Activity>
 </Activities>
 </ElementAtLevel2>  
 </ElementAtLevel1>
 <ExtendedAttributes/>
 </Package>
4
  • 1
    Seems you are confused by namespace and namespace prefix. At least in your first example "ns0" is an namespace prefix. But in the second example "ns0" and "ns1" are uses as uri. So this confuse me.
    – hr_117
    Jun 5, 2013 at 13:41
  • 1
    Thanks you are right and i have updated it Jun 5, 2013 at 15:00
  • When you say "it not able to pick the namespace and as a result transformation is not happening", do you mean that the processor doesn't output any XML? Or that you think it's running the identity template instead of the namespace-changing template? Please show the actual output XML, along with corresponding input XML, so we can diagnose what's happening..
    – LarsH
    Jun 5, 2013 at 15:54
  • i have updated with xslt and xml Jun 5, 2013 at 18:27

1 Answer 1

1

[Revised in light of OP's revision of the question.]

If I understand you correctly, you are hoping that if an element in namespace http://sample2.1 with localname Activity is encountered in the input, then (1) the template with match="*[namespace-uri()='http://sample2.1']" will match it and move it into namespace http://sample2.2, and then (2) the template with match="//xslnsv:Activity" will fire. Is that a correct understanding?

If so, there are two problems here.

First, the namespace-changing template produces a new element node in namespace http://sample2.2, but nothing in the code you show tries to apply any templates to that new element node.

The second problem is that XSLT 1.0 templates match only elements in input documents; they do not and cannot match nodes constructed by the stylesheet. This is one of the big differences between XSLT 1.0 and XSLT 2.0. A common extension to XSLT 1.0 allows created nodes to be matched by templates; look for information on the node-set extension if you want to try that.

A simpler solution is to split your stylesheet into two: one to move elements from the old namespace into the new namespace, and the second to process elements in the new namespace.

(I should also note that I am unable to reproduce your results. When I run the stylesheet you give on the input you give, I get output in namespace http://sample2.2, not in namespace http://sample2.1. I assume you have been the victim of a copy/paste error.)

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.