问题
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