XSLT - 如何在另一个元素中查找最大的日期和位置值

时间:2016-03-11 04:14:37

标签: xml xslt

我有以下XML:

<Loans>
  <Loan>
    <LoanSecondaryStatusDates>
      <LoanSecondaryStatus>
        <StatusName>Status1</StatusName>
        <StatusDate>2015-12-21</StatusDate>
      </LoanSecondaryStatus>
      <LoanSecondaryStatus>
        <StatusName>Status2</StatusName>
        <StatusDate>2015-12-23</StatusDate>
      </LoanSecondaryStatus>
    </LoanSecondaryStatusDates>
    <LoanSecondaryStatus>Need status name from greatest status date value here (in this case Status2 would be the value)</LoanSecondaryStatus>
  </Loan>
</Loans>

当前的XSLT:

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

  <xsl:output indent="yes" />

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


  <xsl:template match="LoanSecondaryStatus[StatusDate='1900-01-01T00:00:00']"/>


</xsl:stylesheet> 

目标: 我需要从“LoanSecondaryStatus”节点中输入最新日期,并将“StatusName”作为“LoanSecondaryStatus”值。如何使用XSLT实现这一目标?

1 个答案:

答案 0 :(得分:0)

使用这种日期格式,您只需按字符串值排序。

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

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

  <xsl:template match="Loan/LoanSecondaryStatus">
    <xsl:copy>
      <xsl:for-each select="/Loans/Loan/LoanSecondaryStatusDates/LoanSecondaryStatus">
        <xsl:sort select="StatusDate" order="descending"/>
        <xsl:if test="position() = 1">
          <xsl:value-of select="StatusName"/>
        </xsl:if>
      </xsl:for-each>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>