使用 foreach 的 XSLT 映射 [英] XSLT Mapping using foreach

查看:22
本文介绍了使用 foreach 的 XSLT 映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This is the sample XML data that i am using and then i have to pass all the values to an additional properties tag..

<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 转换后,我期望格式为

After transformation using XSLT i am expecting the format as

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

我没有得到特定标签的文本值.所有值都重复相同的元素.任何人都可以帮助我了解如何做到这一点.

I am not getting the text value for the specific tag.The same element is repeated for all the values. can anyone help me on this on how to do that.

推荐答案

/ns1:Quote/*/text() 总是选择ns1:Quote<的第一个子元素的文本/代码>.

/ns1:Quote/*/text() always select the text of the first child of ns1:Quote.

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

You can simply use . to get the value of the current node.

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

As Dimitre Novatchev points out in the comments:

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

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:").

In addition, instead of substring-after(name(),"ns1:"), you can simply use local-name().

这篇关于使用 foreach 的 XSLT 映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆