XSLT节点具有一组重复元素

时间:2018-05-06 05:22:36

标签: xslt xslt-2.0

我正在获取多个RetrieveReplyDTO节点并复制estimateNo字段。要求是,我们必须找到我们是否在RetrieveReplyDTO节点下获得任何重复的估计值,如果我们得到任何重复的估计值,那么检查该特定节点的creationDate和creationTime" RetrieveReplyDTO"并获得最新的估计数字。

示例XML:

<?xml version="1.0" encoding="UTF-8" ?><ns0:retrieveResponse xmlns:ns0="some.url">
     <ns0:out>
        <ns0:replyElements>               
           <ns0:RetrieveReplyDTO>
              <ns0:estimateNo>1234</ns0:estimateNo>
              <ns0:estimateStatus>IN</ns0:estimateStatus>
              <ns0:creationDate>20170220</ns0:creationDate>
              <ns0:creationTime>111536</ns0:creationTime>
           </ns0:RetrieveReplyDTO>
           <ns0:RetrieveReplyDTO>
              <ns0:estimateNo>1234</ns0:estimateNo>
              <ns0:estimateStatus>IN</ns0:estimateStatus>
              <ns0:creationDate>20170225</ns0:creationDate>
              <ns0:creationTime>144500</ns0:creationTime>
           </ns0:RetrieveReplyDTO>  
           <ns0:RetrieveReplyDTO>
              <ns0:estimateNo>4567</ns0:estimateNo>
              <ns0:estimateStatus>IN</ns0:estimateStatus>
              <ns0:creationDate>20170230</ns0:creationDate>
              <ns0:creationTime>144500</ns0:creationTime>
           </ns0:RetrieveReplyDTO>
           <ns0:RetrieveReplyDTO>
              <ns0:estimateNo>4567</ns0:estimateNo>
              <ns0:estimateStatus>IN</ns0:estimateStatus>
              <ns0:creationDate>20170229</ns0:creationDate>
              <ns0:creationTime>100045</ns0:creationTime>
           </ns0:RetrieveReplyDTO>                            
        </ns0:replyElements>
     </ns0:out>
  </ns0:retrieveResponse>

在XSLT下面我试过

<xsl:template match="/replyElements">  <xsl:for-each-group select="ns0:RetrieveReplyDTO" group-by="ns0:estimateNo">   
  <xsl:perform-sort select="current-group()">
    <xsl:sort select="ns0:creationDate" order="descending" />
    <xsl:sort select="ns0:creationTime" order="descending" />
  </xsl:perform-sort> 
</xsl:variable>
<xsl:copy-of select="." /></xsl:for-each-group></xsl:template>

我的结果低于结果:

<?xml version = '1.0' encoding = 'UTF-8'?><ns0:retrieveResponse ><ns0:out>
  <ns0:replyElements>
     <ns0:RetrieveReplyDTO>
              <ns0:estimateNo>1234</ns0:estimateNo>
              <ns0:estimateStatus>IN</ns0:estimateStatus>
              <ns0:versionNo>001</ns0:versionNo>
           <ns0:creationDate>20170225</ns0:creationDate>
              <ns0:creationTime>144500</ns0:creationTime>
           </ns0:RetrieveReplyDTO>
     <ns0:RetrieveReplyDTO>
              <ns0:estimateNo>4567</ns0:estimateNo>
              <ns0:estimateStatus>IN</ns0:estimateStatus>
              <ns0:creationDate>20170229</ns0:creationDate>
              <ns0:creationTime>100045</ns0:creationTime>
           </ns0:RetrieveReplyDTO>
  </ns0:replyElements>

它给出了第二个搜索估计数据,没有做任何排序。请帮忙

1 个答案:

答案 0 :(得分:0)

for-each-group内,您可以使用for-eachxsl:sort,然后您可以按排序顺序输出第一项,即更改

<xsl:template match="/replyElements">  <xsl:for-each-group select="ns0:RetrieveReplyDTO" group-by="ns0:estimateNo">   
  <xsl:perform-sort select="current-group()">
    <xsl:sort select="ns0:creationDate" order="descending" />
    <xsl:sort select="ns0:creationTime" order="descending" />
  </xsl:perform-sort> 
</xsl:variable>
<xsl:copy-of select="." /></xsl:for-each-group></xsl:template>

<xsl:template match="replyElements">  
<xsl:for-each-group select="ns0:RetrieveReplyDTO" group-by="ns0:estimateNo">   
  <xsl:for-each select="current-group()">
    <xsl:sort select="ns0:creationDate" order="descending" />
    <xsl:sort select="ns0:creationTime" order="descending" />
    <xsl:if test="position() = 1">
      <xsl:copy-of select="."/>
  </xsl:for-each> 
</xsl:for-each-group>
</xsl:template>

请注意,我还更改了match模式,因为replyElements不是输入代码段中的根元素。