XSLT 连接重复元素的不同值 [英] XSLT to concate distinct values for repeated elements

查看:23
本文介绍了XSLT 连接重复元素的不同值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要求,我将按如下方式获取 XML:

I have a requirement where i will be getting the XML as follows:

<Input>
    <Record>
        <EMPID>FirstEmployee</EMPID>
    </Record>           
    <Record>
        <EMPID>SecondEmployee</EMPID>
    </Record>
    <Record>
        <EMPID>FirstEmployee</EMPID>
    </Record>           
    <Record>
        <EMPID>SecondEmployee</EMPID>
    </Record>
</Input>

应用转换后我对上述 xml 的预期输出是:

My expected output for the xml above after applying transformation is:

<Output>
    <Record>
        <EMPID>FirstEmployee|1</EMPID>
    </Record>
    <Record>
         <EMPID>SecondEmployee|1</EMPID>
    </Record>
    <Record>
         <EMPID>FirstEmployee|2</EMPID>
    </Record>
    <Record>
         <EMPID>SecondEmployee|2</EMPID>
    </Record>
</Output>

当以这种方式使用其中一种解决方案时

When used one of the solutions in this way

<?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="Input" namespace="http://xmlns.oracle.com/UMSAdapterVerify/XSLTCheck/BPELProcess1"/>
    </source>
  </mapSources>
  <mapTargets>
    <target type="WSDL">
      <schema location="../BPELProcess1.wsdl"/>
      <rootElement name="Output" namespace="http://xmlns.oracle.com/UMSAdapterVerify/XSLTCheck/BPELProcess1"/>
    </target>
  </mapTargets>
  <!-- GENERATED BY ORACLE XSL MAPPER 11.1.1.7.8(build 150622.2350.0222) AT [FRI DEC 16 15:55:29 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:client="http://xmlns.oracle.com/UMSAdapterVerify/XSLTCheck/BPELProcess1"
    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: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: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 client plnk xsd wsdl bpws xp20 mhdr bpel oraext dvm hwf med ids bpm xdk xref ora socket ldap">
  <xsl:template match="/">
    <client:Output>
      <xsl:for-each select="/client:Input/client:Record">
        <client:Record>
          <xsl:variable name="current" select="."/>
          <xsl:variable name="pos">
            <xsl:number level="any" count="client:EMPID[.=$current]"/>
          </xsl:variable>
          <client:EMPID>
            <xsl:value-of select='concat(.,"|",$pos)'/>
          </client:EMPID>
        </client:Record>
      </xsl:for-each>
    </client:Output>
  </xsl:template>
</xsl:stylesheet>

输出为

<Output>
    <client:Record>
        <client:EMPID> Srinivas |0</client:EMPID>
    </client:Record>
    <client:Record>
        <client:EMPID> kalyan |0</client:EMPID>
    </client:Record>
    <client:Record>
        <client:EMPID> Srinivas |0</client:EMPID>
    </client:Record>
    <client:Record>
        <client:EMPID> kalyan |0</client:EMPID>
    </client:Record>
</Output>

谁能帮忙解决这个问题

推荐答案

这是一个使用 的选项xsl:number...

XML 输入

<Input>
    <Record>
        <EMPID>FirstEmployee</EMPID>
    </Record>           
    <Record>
        <EMPID>SecondEmployee</EMPID>
    </Record>
    <Record>
        <EMPID>FirstEmployee</EMPID>
    </Record>           
    <Record>
        <EMPID>SecondEmployee</EMPID>
    </Record>
</Input>

XSLT 1.0

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="/Input">
    <Output>
      <xsl:apply-templates/>
    </Output>
  </xsl:template>

  <xsl:template match="EMPID">
    <xsl:variable name="current" select="."/>
    <xsl:variable name="pos">
      <xsl:number level="any" count="EMPID[.=$current]"/>
    </xsl:variable>
    <xsl:copy>
      <xsl:value-of select="concat(.,'|',$pos)"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

XML 输出

<Output>
   <Record>
      <EMPID>FirstEmployee|1</EMPID>
   </Record>
   <Record>
      <EMPID>SecondEmployee|1</EMPID>
   </Record>
   <Record>
      <EMPID>FirstEmployee|2</EMPID>
   </Record>
   <Record>
      <EMPID>SecondEmployee|2</EMPID>
   </Record>
</Output>

<小时>

编辑更新的问题...

您的 xsl:for-each 正在选择 client:Record,因此您需要更新 current 变量以选择子 客户端:EMPID.您还应该计算 client:Record 元素而不是 client:EMPID.

Your xsl:for-each is selecting client:Record, so you need to update the current variable to select the child client:EMPID. You should also count the client:Record elements instead of client:EMPID.

示例

<xsl:template match="/">
  <client:Output>
    <xsl:for-each select="client:Input/client:Record">
      <client:Record>
        <xsl:variable name="current" select="client:EMPID"/>
        <xsl:variable name="pos">
          <xsl:number level="any" count="client:Record[client:EMPID=$current]"/>
        </xsl:variable>
        <client:EMPID>
          <xsl:value-of select='concat(.,"|",$pos)'/>
        </client:EMPID>
      </client:Record>
    </xsl:for-each>
  </client:Output>
</xsl:template>

这篇关于XSLT 连接重复元素的不同值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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