使用 foreach 的 XSLT 映射

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>

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

/ns1:Quote/*/text() 总是 select ns1:Quote 的第一个 child 的文本。

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

正如 Dimitre Novatchev 在评论中指出的那样:

text() ... selects all text-node-children of the context node. Then the string-value of the first of these is used (in XPath 1.0, in XPath 2.0 this most often results in type error -- a sequence of nodes passed when a single node expected). If we know that there is a single text-node child, it is the shortest to use a dot -- . which stands for the context node

此外,您可以简单地使用 local-name()

而不是 substring-after(name(),"ns1:")