Xslt : Create root element (Starting Tag )dynamically

女生的网名这么多〃 提交于 2019-12-14 03:01:10

问题


Following are the nodes in XML Data

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

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>

Please help me out


回答1:


This transformation:

<xsl:stylesheet version="1.0"   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:ext="http://exslt.org/common"
     xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
     exclude-result-prefixes="ext soapenv">
        <xsl:output omit-xml-declaration="yes" indent="yes"/>
      <xsl:param name="pUserName" select="'nida'"/>
      <xsl:param name="pPassword" select="'123'"/>
      <xsl:param name="pWebServiceUrl" select="'http://webser.part.site'"/>

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

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

     <xsl:template match="/*">
       <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
           <xsl:copy-of select="$vNS"/>
         <soapenv:Body>
           <xsl:element name="web:upload" namespace="{$vNS}">
             <xsl:element name="web:username" namespace="{$vNS}">
               <xsl:value-of select="$pUserName"/>
             </xsl:element>
             <xsl:element name="web:password" namespace="{$vNS}">
               <xsl:value-of select="$pPassword"/>
             </xsl:element>
           </xsl:element>
         </soapenv:Body>
         </soapenv:Envelope>
     </xsl:template>
</xsl:stylesheet>

when applied on any XML document (not used), produces the wanted, correct result:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://webser.part.site">
   <soapenv:Body>
      <web:upload>
         <web:username>nida</web:username>
         <web:password>123</web:password>
      </web:upload>
   </soapenv:Body>
</soapenv:Envelope>


来源:https://stackoverflow.com/questions/11808073/xslt-create-root-element-starting-tag-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!