xml – Xslt:动态创建命名空间

以下是XML数据中的节点
<WebServiceUrl>"http://webser.part.site"</WebServiceUrl>
<UserName>nida</UserName>
<Passsword>123</Password>

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

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

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

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

所以我想从XSLT Code获得的最终outPut如下:

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

当我尝试保存此代码时,由于缺少此节点的起始标记,因此会出现错误

</soapenv:Envelope>

请在此更改我的错误.

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’命名空间):

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

II.这个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"/>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念
xml文件介绍及使用
xml编程(一)-xml语法
XML文件结构和基本语法
第2章 包装类
XML入门的常见问题(二)
Java对象的强、软、弱和虚引用
JS解析XML文件和XML字符串详解
java中枚举的详细使用介绍
了解Xml格式
XML入门的常见问题(四)
深入SQLite多线程的使用总结详解
PlayFramework完整实现一个APP(一)
XML和YAML的使用方法
XML轻松学习总节篇