XSLT Mapping using foreach



这是我正在使用的示例XML数据,然后我必须将所有值传递给其他属性标记。

<ns1:Quote>
<ns1:QuoteVendor>123</ns1:QuoteVendor>
<ns1:QuoteNumber>sai</ns1:QuoteNumber>
<ns1:QuoteVersion>sri</ns1:QuoteVersion>
<ns1:QuoteValue>sas</ns1:QuoteValue>
<ns1:QuoteProperty>sandy</ns1:QuoteProperty>
</ns1:Quote>

使用 XSLT 进行转换后,我希望格式为

<AdditionalProperties>
<ns1:Properties>
<ns1:Propertyname>QuoteVendor</ns1:Propertyname>
<ns1:propertyValue>123</ns1:propertyValue>
</ns1:Properties>
<ns1:Properties>
<ns1:Propertyname>QuoteNumber</ns1:Propertyname>
<ns1:propertyValue>sai</ns1:propertyValue>
</ns1:Properties>
<ns1:Properties>
<ns1:Propertyname>QuoteVersion</ns1:Propertyname>
<ns1:propertyValue>sri</ns1:propertyValue>
</ns1:Properties>
<ns1:Properties>
<ns1:Propertyname>QuoteValue</ns1:Propertyname>
<ns1:propertyValue>sas</ns1:propertyValue>
</ns1:Properties>
<ns1:Properties>
<ns1:Propertyname>QuoteProperty</ns1:Propertyname>
<ns1:propertyValue>sandy</ns1:propertyValue>
</ns1:Properties>
</AdditionalProperties>

我目前使用的转换是:

<?xml version="1.0" encoding="UTF-8" ?>
<?oracle-xsl-mapper
<!-- SPECIFICATION OF MAP SOURCES AND TARGETS, DO NOT MODIFY. -->
<mapSources>
<source type="WSDL">
<schema location="../BPELProcess1.wsdl"/>
<rootElement name="Quote" namespace="http://www.example.org"/>
</source>
</mapSources>
<mapTargets>
<target type="WSDL">
<schema location="../BPELProcess1.wsdl"/>
<rootElement name="AdditionalProperties" namespace="http://www.example.org"/>
</target>
</mapTargets>
<!-- GENERATED BY ORACLE XSL MAPPER 11.1.1.7.8(build 150622.2350.0222) AT [THU NOV 24 17:33:14 IST 2016]. -->
?>
<xsl:stylesheet version="1.0"
xmlns:bpws="http://schemas.xmlsoap.org/ws/2003/03/business-process/"
xmlns:xp20="http://www.oracle.com/XSL/Transform/java/oracle.tip.pc.services.functions.Xpath20"
xmlns:mhdr="http://www.oracle.com/XSL/Transform/java/oracle.tip.mediator.service.common.functions.MediatorExtnFunction"
xmlns:bpel="http://docs.oasis-open.org/wsbpel/2.0/process/executable"
xmlns:oraext="http://www.oracle.com/XSL/Transform/java/oracle.tip.pc.services.functions.ExtFunc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ns1="http://www.example.org"
xmlns:dvm="http://www.oracle.com/XSL/Transform/java/oracle.tip.dvm.LookupValue"
xmlns:hwf="http://xmlns.oracle.com/bpel/workflow/xpath"
xmlns:plnk="http://docs.oasis-open.org/wsbpel/2.0/plnktype"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:med="http://schemas.oracle.com/mediator/xpath"
xmlns:ids="http://xmlns.oracle.com/bpel/services/IdentityService/xpath"
xmlns:bpm="http://xmlns.oracle.com/bpmn20/extensions"
xmlns:xdk="http://schemas.oracle.com/bpel/extension/xpath/function/xdk"
xmlns:xref="http://www.oracle.com/XSL/Transform/java/oracle.tip.xref.xpath.XRefXPathFunctions"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:client="http://xmlns.oracle.com/add/MultiplevaluesTest/BPELProcess1"
xmlns:ora="http://schemas.oracle.com/xpath/extension"
xmlns:socket="http://www.oracle.com/XSL/Transform/java/oracle.tip.adapter.socket.ProtocolTranslator"
xmlns:ldap="http://schemas.oracle.com/xpath/extension/ldap"
exclude-result-prefixes="xsi xsl ns1 plnk xsd wsdl client bpws xp20 mhdr bpel oraext dvm hwf med ids bpm xdk xref ora socket ldap">
<xsl:template match="/">
<ns1:AdditionalProperties>
<xsl:for-each select="/ns1:Quote/*">
<xsl:choose>
<xsl:when test="(position() = 1.0) or (position() = 3.0)">
<ns1:Properties>
<ns1:Propertyname>
<xsl:value-of select='substring-after(name(),"ns1:")'/>
</ns1:Propertyname>
<ns1:propertyValue>
<xsl:value-of select='/ns1:Quote/*/text()'/>
</ns1:propertyValue>
</ns1:Properties>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</ns1:AdditionalProperties>
</xsl:template>
</xsl:stylesheet>

使用后输出为

<AdditionalProperties>
<ns1:Properties>
<ns1:Propertyname>QuoteVendor</ns1:Propertyname>
<ns1:propertyValue>123</ns1:propertyValue>
</ns1:Properties>
<ns1:Properties>
<ns1:Propertyname>QuoteVersion</ns1:Propertyname>
<ns1:propertyValue>123</ns1:propertyValue>
</ns1:Properties>
</AdditionalProperties>

我没有获得特定标签的文本值。对所有值重复相同的元素。 谁能帮我解决这个问题。

/ns1:Quote/*/text()始终选择ns1:Quote的第一个子项的文本。

您可以简单地使用.来获取当前节点的值。

正如迪米特雷·诺瓦切夫(Dimitre Novatchev)在评论中指出的那样:

text()......选择上下文节点的所有文本节点子节点。然后使用其中第一个的字符串值(在 XPath 1.0 中,在 XPath 2.0 中,这最常导致类型错误 - 当单个节点预期时传递的节点序列)。如果我们知道有一个文本节点子节点,那么使用点是最短的 --.代表上下文节点

此外,您可以简单地使用local-name()代替substring-after(name(),"ns1:")

最新更新