首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用foreach的XSLT映射

使用foreach的XSLT映射
EN

Stack Overflow用户
提问于 2016-11-24 12:57:40
回答 1查看 1.1K关注 0票数 0

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

代码语言:javascript
复制
<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进行转换之后,我期望格式为

代码语言:javascript
复制
<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>

我目前使用的转换是:

代码语言:javascript
复制
<?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>

使用后的输出是

代码语言:javascript
复制
<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获取文本值,对于所有的值都会重复相同的元素。有人能帮我解决这个问题吗。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-24 13:37:56

/ns1:Quote/*/text()总是选择ns1:Quote的第一个子级的文本。

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

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

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

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40787013

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档