2016-12-16 44 views
0

我有一个要求,其中i将被获取XML如下:XSLT到concate不同值重复元素

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

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

当以这种方式使用的解决方案之一

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

这个

+0

我的代码中没有38行(或42或44)。 –

+0

我已更新问题。请查看此 –

回答

0

下面是一个使用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变量来选择孩子client:EMPID。您还应该计算client:Record元素而不是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> 
+0

我已更新问题。我已使用您的方法,但价值未得到体现。 –

+0

@srinivaskalyan - 请参阅我的答案更新。 –

0

任何人的帮助可以试试这个:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

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

<xsl:template match="EMPID"> 
    <xsl:variable name="varPresentValue"><xsl:value-of select="."/></xsl:variable> 
    <xsl:copy> 
     <xsl:value-of select="concat(., '|', count(preceding::EMPID[.=$varPresentValue])+1)"/> 
    </xsl:copy> 
</xsl:template> 

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

</xsl:stylesheet> 
0

如果你想要的是不同值,你可以做简单:

XSLT 1.0

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

<!-- identity transform --> 
<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:copy> 
     <xsl:value-of select="."/> 
     <xsl:text>|</xsl:text> 
     <xsl:value-of select="generate-id()"/>  
    </xsl:copy> 
</xsl:template> 

</xsl:stylesheet> 

这比更有效反复计数前面的兄弟姐妹或编号类似的节点。

+0

@srinivaskalyan你的问题被标记为'XSLT'和上述工作在XSLT中:http://xsltransform.net/bFWR5En。如果您使用的是不合格的处理器,或者将我的代码添加到您的代码中,则我不感兴趣。 –

+0

谢谢@ michael.hor257k ...我将在Oracle SOA中实际使用转换活动。复制申请模板在这里不起作用。我会尝试使用你的想法并尝试在这里实现它。 –