使用xslt以XML格式排序日期

时间:2013-07-02 08:40:53

标签: xslt-1.0

我想使用xslt对xml中的日期进行排序,并且我的日期元素在xsd中使用数据类型进行验证,日期如下,是我的xml和xsl

XML

<Trade xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<Symbol xsi:type="TradedSymbol"    Type="Swap">
 <Economic xsi:type="EconomicLeg">
  <Leg xsi:type="IRLegGeneratedFixed">
    <Schedule xsi:type="ScheduleGeneratorFixed">
     <Date>2014-06-17</Date>
    </Schedule>
  </Leg>
 </Economic>
</Symbol>
<Symbol xsi:type="TradedSymbol"    Type="Swap">
 <Economic xsi:type="EconomicDetailIRLeg">
  <Leg xsi:type="IRLegFloat">
   <Schedule xsi:type="ScheduleGeneratorFloat">
    <Date>2018-06-17</Date>
   </Schedule>
  </Leg>
 </Economic>
</Symbol>
<Symbol xsi:type="TradedSymbol" Type="Floor">
 <Economic xsi:type="EconomicDetailIRLeg">
  <Leg xsi:type="IRLegFloat">
   <Schedule xsi:type="ScheduleGeneratorFloat">
    <Date>2000-06-17</Date>
  </Schedule>
 </Leg>
 </Economic>
</Symbol>
</Trade>

XSD

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template name="fiExoticStructuredDeal">
<fixedIncomeExoticDeal>
  <frontOfficeDealDescription>Empty   decription</frontOfficeDealDescription>
 <component>
   <frontOfficeComponentType>
    <optionDetails>
    <optionStyle>European</optionStyle>
     <optionDates>
   <adjustedDate>
<xsl:for-each select="/Trade/Symbol/Economic/Leg/Schedule">

   <xsl:sort select="concat(substring-before(Date,'-'), substring-before(substring-after(Date,'-    '),'-'),substring-after(substring-after(Date,'-'),'-'))" order="ascending"/>
    <xsl:choose>
    <xsl:when test="position() = last()"> 
       <xsl:value-of select="Date"/>
                                            </xsl:when>
            </xsl:choose> 

</xsl:for-each>

          </adjustedDate>
     </optionDates>
</optionDetails>
</frontOfficeComponentType>
    </component>

    </fixedIncomeExoticDeal>

</xsl:template>

</xsl:stylesheet>

我无法使用xslt对Date进行排序。我总是在Schedule元素中给出最后一个日期。 如果有人让我知道我是否遗漏了某些东西,那将是很棒的。

1 个答案:

答案 0 :(得分:0)

使用

<xsl:sort select="Date" order="ascending"/>