xsl-list
[Top] [All Lists]

Re: [xsl] moving namespace to soap envelope declaration and changing the namespace prefix inbody

2012-10-22 20:23:29
At 2012-10-23 00:51 +0800, ram wrote:
     I have soap request which looks like the following

Input:
...

I need to move the namespace of SubHeader to soap envelope declaration and change the namespace prefix of all the child nodes of of soap body to v11.

I'm interested to read you say the word "need", when a true XML application does not "need" prefixes of any kind ... it needs appropriate namespace URI strings. The output you cite is equivalent to the input you have if you look at the expanded names of the elements. The prefixes do not matter to an XML-aware application and thus should not matter to you.

However, what you ask for can be done.

Output:
...

    I tried to do it like the following
...
<xsl:stylesheet version="1.0"

I note you are using XSLT 1.0, so in my solution below I limited myself to XSLT 1.0.

<!-- special handling for soap:Header -->
<xsl:template match="*[local-name()='Header' and namespace-uri()='http://schemas.xmlsoap.org/soap/envelope/']">

That match pattern is poorly written. You can use any prefix you want in your stylesheet, it does not have to match the prefix used in the source document. You could have written the above as follows by binding the URI to a prefix "sally" and then using that prefix:

   ... xmlns:sally="http://schemas.xmlsoap.org/soap/envelope/"; ...
   <xsl:template match="sally:Header">

The way I wrote the match will still match your input, independent of the prefix used for the input. You should never write your match statement the way you did as you are asking the processor to do extra work (and it is difficult to maintain).

<xsl:variable name="suHeader" select="value of subHeader"/>
<xsl:copy>

Here you are copying the namespace nodes of the source, and you need to avoid that in order to take control over the prefixes.

    the output of this xsl is coming as Input, how can i change it the output

I hope the working example below helps.

. . . . . . . . Ken

T:\ftemp>type ram.xml
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:v1="http://abcd.com/v1"; xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/";>
<soapenv:Header>
<v1:SubHeader xmlns:v1="http://abcd.com/test/eia/v1";>
</v1:SubHeader>
</soapenv:Header>
<soapenv:Body>
<v1:CreateResponse>
</v1:CreateResponse>
</soapenv:Body>
</soapenv:Envelope>

T:\ftemp>xslt ram.xml ram.xsl
<?xml version="1.0" encoding="utf-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"; xmlns:v11="http://abcd.com/v1"; xmlns:v1="http://abcd.com/test/eia/v1";>
<soapenv:Header>
<v1:SubHeader>
</v1:SubHeader>
</soapenv:Header>
<soapenv:Body>
<v11:CreateResponse>
</v11:CreateResponse>
</soapenv:Body>
</soapenv:Envelope>
T:\ftemp>type ram.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  xmlns:v11="http://abcd.com/v1";
  xmlns:v1="http://abcd.com/test/eia/v1";
  version="1.0">

<!--preserve the document element and add arbitrary namespace prefixes-->
<xsl:template match="/*">
  <xsl:element name="{name(.)}" namespace="{namespace-uri(.)}">
    <xsl:copy-of select="document('')/*/namespace::*[name(.)!='xsl']"/>
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates/>
  </xsl:element>
</xsl:template>

<!--reconstitute elements with a hardwired prefix on output-->
<xsl:template match="v11:*">
  <xsl:element name="v11:{local-name(.)}" namespace="{namespace-uri(.)}">
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates/>
  </xsl:element>
</xsl:template>

<!--reconstitute elements with a hardwired prefix on output-->
<xsl:template match="v1:*">
  <xsl:element name="v1:{local-name(.)}" namespace="{namespace-uri(.)}">
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates/>
  </xsl:element>
</xsl:template>

<!--every other element can be reconstituted ignoring attached namespaces-->
<xsl:template match="*">
  <xsl:element name="{name(.)}" namespace="{namespace-uri(.)}">
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates/>
  </xsl:element>
</xsl:template>

</xsl:stylesheet>
T:\ftemp>


--
Contact us for world-wide XML consulting and instructor-led training
Free 5-hour lecture: http://www.CraneSoftwrights.com/links/udemy.htm
Crane Softwrights Ltd.            http://www.CraneSoftwrights.com/s/
G. Ken Holman                   mailto:gkholman(_at_)CraneSoftwrights(_dot_)com
Google+ profile: https://plus.google.com/116832879756988317389/about
Legal business disclaimers:    http://www.CraneSoftwrights.com/legal


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>