使用xsl将xml属性中的节点添加到soap消息

使用xsl将xml属性中的节点添加到soap消息,xml,xslt,properties,add,Xml,Xslt,Properties,Add,求你了,我需要你的帮助 XML:我有一条SOAP消息: <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <soapenv:Header/> <soapenv:Body> <Service> <ServiceRequest> <tag3>value3</tag3> </ServiceReq

求你了,我需要你的帮助

XML:我有一条SOAP消息:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
 <Service>
  <ServiceRequest>
   <tag3>value3</tag3>
  </ServiceRequest>
 </Service>
</soapenv:Body>
</soapenv:Envelope>

价值3
XSL:我需要从xml属性添加节点。我尝试使用以下xsl:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">
<xsl:output encoding="UTF-8" indent="yes" method="xml" standalone="no" omit-xml-declaration="no"/>
<xsl:variable name="props">
<properties>
 <property>
  <key>tag1</key>
  <value>value1</value>
 </property>
 <property>
  <key>tag2</key>
  <value>value2</value>
 </property>
</properties>
</xsl:variable>

<xsl:template match="@* | node()">
<xsl:copy>
 <xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>

<xsl:template match="ServiceRequest">
<xsl:copy>
 <xsl:apply-templates select="@* | node()" />
  <xsl:for-each select="$props/properties/property">
   <xsl:variable name="tag" select="key" />
   <xsl:element name="{$tag}">
    <xsl:value-of select="value" />
   </xsl:element>
  </xsl:for-each>
 </xsl:copy>
</xsl:template>
</xsl:stylesheet>

tag1
价值1
tag2
价值2
XSL后的XML:我需要这样的结果:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
 <Service>
  <ServiceRequest>
   <tag1>value1</tag1>
   <tag2>value2</tag2>
   <tag3>value3</tag3>
  </ServiceRequest>
 </Service>
</soapenv:Body>
</soapenv:Envelope>
 <xsl:stylesheet 
  version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
  exclude-result-prefixes="msxsl">

  <xsl:output encoding="UTF-8" indent="yes" method="xml" standalone="no" omit-xml-declaration="no"/>
  <xsl:variable name="props">
    <properties>
      <property>
        <key>tag1</key>
        <value>value1</value>
      </property>
      <property>
        <key>tag2</key>
        <value>value2</value>
      </property>
    </properties>
  </xsl:variable>

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()" />
    </xsl:copy>
  </xsl:template>

  <xsl:template match="ServiceRequest">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()" />
      <xsl:for-each select="msxsl:node-set($props)/properties/property">
        <xsl:variable name="tag" select="key" />
        <xsl:element name="{$tag}">
          <xsl:value-of select="value" />
        </xsl:element>
      </xsl:for-each>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

价值1
价值2
价值3

但是XSL不起作用。您能帮助我吗?

问题是XSLT有一个默认名称空间
xmlns=”http://www.w3.org/1999/xhtml
,因此
属性
属性
元素位于该命名空间中,因此
选择=“$props/properties/property“
不匹配任何内容,因为他们正在查找空命名空间中的元素

默认名称空间似乎不是必需的,因此最简单的修复方法就是删除它

另一个可能的问题是
$prop
变量包含一个XML片段,而不是一个节点集-这意味着在
中为每个
使用它之前,需要将其转换为一个节点集-这是通过依赖于实现的XSLT扩展函数完成的

使用Microsoft XSLT处理器,正确的XSLT如下所示:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
 <Service>
  <ServiceRequest>
   <tag1>value1</tag1>
   <tag2>value2</tag2>
   <tag3>value3</tag3>
  </ServiceRequest>
 </Service>
</soapenv:Body>
</soapenv:Envelope>
 <xsl:stylesheet 
  version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
  exclude-result-prefixes="msxsl">

  <xsl:output encoding="UTF-8" indent="yes" method="xml" standalone="no" omit-xml-declaration="no"/>
  <xsl:variable name="props">
    <properties>
      <property>
        <key>tag1</key>
        <value>value1</value>
      </property>
      <property>
        <key>tag2</key>
        <value>value2</value>
      </property>
    </properties>
  </xsl:variable>

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()" />
    </xsl:copy>
  </xsl:template>

  <xsl:template match="ServiceRequest">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()" />
      <xsl:for-each select="msxsl:node-set($props)/properties/property">
        <xsl:variable name="tag" select="key" />
        <xsl:element name="{$tag}">
          <xsl:value-of select="value" />
        </xsl:element>
      </xsl:for-each>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

tag1
价值1
tag2
价值2