sorting dates in XML using xslt

匿名 (未验证) 提交于 2019-12-03 01:42:02

问题:

I want to sort the dates in xml using xslt and my date element is validated in xsd with datatype as date below is my xml and 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> 

I am not able sort the Date using xslt.It always gives me the last date in Schedule element. It will be great if someone let me know if i am missing something.

回答1:

Use

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


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!