Xslt :动态创建命名空间 [英] Xslt : Create a namespace dynamically

查看:24
本文介绍了Xslt :动态创建命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是 XML Data 中的节点

Following are the nodes in XML Data

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

我已将此节点值传递给 Xslt 服务,现在我在参数 e-g 中有此 url NODE 值

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"/>

现在我想创建一个 soapenv:Envelope 标签并在其中使用这个值

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">

所以我想从 XSLT 代码中得到的最终输出如下:

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>

非常感谢您的帮助.

这是您的代码:

 <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.

推荐答案

I.这个 XSLT 2.0 转换:

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

应用于提供的 XML 文档时:

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

产生想要的、正确的结果(根据参数值产生的web"命名空间):

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"/>

<小时>

二.此 XSLT 1.0 转换:

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

当应用于同一个 XML 文档(上图)时,再次产生想要的、正确的结果:

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

这篇关于Xslt :动态创建命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆