views:

403

answers:

1

I'm trying to add Soap headers to my document and update the first RS node with

 <Rs xmlns="http://tempuri.org/schemas"&gt;

all while copying the remainder of the document nodes. In my real example i'll have more nodes within RS parent node so i'm looking for a solution with a deep copy of some sort.

<-- this is the data which needs transforming -->

<Rs>
   <ID>934</ID>
   <Dt>995116</Dt>
   <Date>090717180408</Date>
   <Code>9349</Code>
   <Status>000</Status>
</Rs>


 <-- Desired Result -->

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"&gt;  
<SOAP-ENV:Body>
  <Rs xmlns="http://tempuri.org/schemas"&gt;
    <ID>934</ID>
    <Dt>090717180408</Dt>
    <Code>9349</Code>
    <Status>000</Status>    
    </Rs>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

<-- this is my StyleSheet. it's not well formed so i can't exexute it-->

<?xml version="1.0"?>
 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="xml" indent="yes" encoding="UTF-8"/>
<xsl:template match="/">
 <SOAP-ENV:Envelope
                 xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"&gt;
  <SOAP-ENV:Body>
     <xsl:apply-templates  select = "Rs">
     </xsl:apply-templates>
     <xsl:copy-of select="*"/>
  </SOAP-ENV:Body>
 </SOAP-ENV:Envelope>
</xsl:template>
<xsl:template match ="Rs">
    <Rs xmlns="http://tempuri.org/schemas"&gt;
</xsl:template>
</xsl:stylesheet>

I've been reading tutorials, but having troubles getting my head around templates and where to implement them.

+1  A: 

xmlns isn't just another attribute, but denotes a namespace change. So it's a little trickier. Try this:

<?xml version='1.0' ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    <xsl:output method="xml" indent="yes" encoding="UTF-8"/>
    <xsl:template match="/">
        <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"&gt;
            <SOAP-ENV:Body>
                <xsl:apply-templates select="Rs"/>
            </SOAP-ENV:Body>
        </SOAP-ENV:Envelope>
    </xsl:template>
    <xsl:template match="node()">
        <xsl:element name="{local-name(.)}" namespace="http://tempuri.org/schemas"&gt;
            <!-- the above line is the tricky one. We can't copy an element from -->
            <!-- one namespace to another, but we can create a new one in the -->
            <!-- proper namespace. -->
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="node()|*"/>
        </xsl:element>
    </xsl:template>
    <xsl:template match="text()">
        <xsl:if test="normalize-space(.) != ''">
            <xsl:value-of select="."/>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

Some of the gymnastics aren't so important if you don't use indent="yes" but I tried to make it match your output as closely as possible.

lavinio