0

Following are the nodes in XML Data

<WebServiceUrl>"http://webser.part.site"</WebServiceUrl>
<UserName>nida</UserName>
<Passsword>123</Password>

I have passed this node value to Xslt Service now i have this url NODE value in parameter e-g

    <xsl:param name="UserName"/>
    <xsl:param name="Password"/>
    <xsl:param name="WebServiceUrl"/>

Now i want to create a soapenv:Envelope tag and use this value in it

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="$WebServiceUrl">

So the final outPut which i want from XSLT Code is as :

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"     xmlns:web="http://webservice2.partner.insite">
<soapenv:Header/>
<soapenv:Body>
<web:upload>
<web:username>nida</web:username>
<web:password>123</web:password>
</web:upload></soapenv:Body></soapenv:Envelope>

Thanks alot for your help .

This is your code :

 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
 <xsl:output method="xml" encoding="UTF-8" omit-xml-declaration="yes"/>
 <xsl:param name="UserName"/>
 <xsl:param name="Password"/>
  <xsl:param name="WebServiceUrl" select="'some: namespace'"/>
<xsl:template match="/">    
SOAPAction: "urn:upload"
Content-Type: text/xml;charset=UTF-8
 <xsl:text>
 </xsl:text>
  <xsl:element name="{name()}"
      namespace="http://schemas.xmlsoap.org/soap/envelope/">
  <xsl:sequence select="namespace::*[not(name()='web')]"/>
  <xsl:namespace name="web" select="$WebServiceUrl"/>
 </xsl:element>
 <xsl:text>
   </xsl:text>
<soapenv:Header/>
   <xsl:text>
   </xsl:text>
<soapenv:Body>
   <xsl:text>
   </xsl:text>
    <web:upload>
   <xsl:text>
   </xsl:text>      
        <web:username><xsl:value-of select="$UserName"/>                </web:username>
    <xsl:text>
   </xsl:text>
                <web:password><xsl:value-of select="$Password"/>           </web:password>
    <xsl:text>
   </xsl:text>
  </soapenv:Envelope>
</xsl:template>
    </xsl:stylesheet>

When i try to save this code it thorws an error as starting tag of this node is missing

</soapenv:Envelope>

Please make changes in this what i am doing wrong in this.

Azeem
  • 2,904
  • 18
  • 54
  • 89
  • How would the new namespace be generated if there is no string ""webservice2" contained in the parameter `$WebServiceUrl`? This isn't logical. – Dimitre Novatchev Aug 03 '12 at 18:31
  • Ok we can make $WebServiceUrl value string as i updated . – Azeem Aug 03 '12 at 18:42
  • My main concern is to pick the Url Value from parameter we have option to make WeserviceUrl value like any one of these: "http://webser.part.site" OR web="http://webservice2.partner.insite" OR xmlns:web="http://webservice2.partner.insite" But At the end what i need is this : – Azeem Aug 03 '12 at 18:46
  • So, what is the problem with my solution? It does exactly that -- creates a namespace dynamically from the namespace-uri provided in a global parameter. – Dimitre Novatchev Aug 03 '12 at 18:51
  • Please have a look on above code – Azeem Aug 03 '12 at 19:20
  • With all due respect, it is elementary to reuse the solution from my answer -- you should be able to do so yourself. Your question has been cfully answered -- if you have other problems, please, ask a new question. – Dimitre Novatchev Aug 03 '12 at 20:01

1 Answers1

3

I. This XSLT 2.0 transformation:

<xsl:stylesheet version="2.0"   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:param name="pUrl" select="'some: namespace'"/>

 <xsl:template match="/*">
     <xsl:element name="{name()}"
          namespace="http://schemas.xmlsoap.org/soap/envelope/">
      <xsl:sequence select="namespace::*[not(name()='web')]"/>
      <xsl:namespace name="web" select="$pUrl"/>
     </xsl:element>
 </xsl:template>
</xsl:stylesheet>

when applied on the provided XML document:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
                  xmlns:web="http://webser.part.site"/>

produces the wanted, correct result (the 'web' namespace produced from the value of a parameter):

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
                  xmlns:web="some: namespace"/>

II. This XSLT 1.0 transformation:

<xsl:stylesheet version="1.0"   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:ext="http://exslt.org/common" exclude-result-prefixes="ext">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:param name="pUrl" select="'some: namespace'"/>

    <xsl:variable name="vrtfDummy">
     <xsl:element name="web:dummy" namespace="{$pUrl}"/>
    </xsl:variable>

    <xsl:variable name="vNS" select="ext:node-set($vrtfDummy)/*/namespace::web"/>

 <xsl:template match="/*">
     <xsl:element name="{name()}"
          namespace="http://schemas.xmlsoap.org/soap/envelope/">
      <xsl:copy-of select="namespace::*[not(name()='web')]"/>
      <xsl:copy-of select="$vNS"/>
     </xsl:element>
 </xsl:template>
</xsl:stylesheet>

when applied on the same XML document (above), again produces the wanted, correct result:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="some: namespace"/>
Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
  • Note also that if you need to generate elements in the dynamically-selected "web" namespaces within the SOAP message body, you will need to do this using xsl:element with a namespace attribute. You can't just rely on using the prefix "web" and having its binding inherited from a parent element: XSLT needs to know the namespace of an element it is creating explicitly. – Michael Kay Aug 03 '12 at 13:25
  • @MichaelKay, Yes, the provided XML document has just a top element and nothing else... Or do I understand you at all? – Dimitre Novatchev Aug 03 '12 at 13:27
  • @DimitreNovatchev , soory i could not convery my message clearly , I have following node http://webser.part.site I just want to append the WebServiceUrl value in the Following Node using XSLT > – Azeem Aug 03 '12 at 18:00
  • @NidaSulheri: Sorry, but I don't understand your last comment. Please, edit the question and specify the exact wanted result (as XML). – Dimitre Novatchev Aug 03 '12 at 18:06
  • @DimitreNovatchev I have updated my question , please help me out and tell me if further clarification is required. – Azeem Aug 03 '12 at 18:24