[英]Add nodes from xml properties to soap message with xsl
拜托我需要你的帮忙,
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>
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>
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不起作用。 你能帮我吗?
问题是XSLT具有默认名称空间xmlns="http://www.w3.org/1999/xhtml
,因此properties
和property
元素位于该名称空间中,因此请select="$props/properties/property"
不匹配任何内容,因为它们正在寻找null命名空间中的元素。
默认名称空间似乎不是必需的,因此最简单的解决方法是删除它。
另一个可能的问题是$prop
变量包含XML片段而不是节点集-这意味着在for-each
使用它之前,需要将其转换为节点集-这是通过XSLT扩展功能完成的取决于实现。
使用Microsoft XSLT处理器,正确的XSLT如下所示:
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.