我使用xml和xslt创建了html文件。 我需要使用xslt的最小日期。这里我的任务是我有一个xml文件,其中包含不同的节点,如flightList,HotelList,CarList,....我需要从所有节点(flightList,HotelList,CarList)获取最低日期。 我已经创建了如下所述的应用程序。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt">
<xsl:output method="xml" indent="yes"/>
<!--<xsl:template match="TRSummary">-->
<!--Flight TravelDate Minimum date-->
<xsl:variable name="Flight">
<xsl:for-each select="TRSummary/FlightList">
<xsl:sort select="number(substring(TravelDate, 7, 4))" order="ascending"/>
<xsl:sort select="number(substring(TravelDate, 3, 2))" order="ascending"/>
<xsl:sort select="number(substring(TravelDate, 1, 2))" order="ascending"/>
<xsl:if test="position() = 1">
<xsl:value-of select="TravelDate"/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<!--Hotel CheckInDate Minimum date-->
<xsl:variable name="Hotel">
<xsl:for-each select="TRSummary/HotelsList">
<xsl:sort select="number(substring(CheckInDate, 7, 4))" order="ascending"/>
<xsl:sort select="number(substring(CheckInDate, 3, 2))" order="ascending"/>
<xsl:sort select="number(substring(CheckInDate, 1, 2))" order="ascending"/>
<xsl:if test="position() = 1">
<xsl:value-of select="CheckInDate"/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<!--Car CarFromDate Minimum date-->
<xsl:variable name="Car">
<xsl:for-each select="TRSummary/CarList">
<xsl:sort select="number(substring(CarFromDate, 7, 4))" order="ascending"/>
<xsl:sort select="number(substring(CarFromDate, 3, 2))" order="ascending"/>
<xsl:sort select="number(substring(CarFromDate, 1, 2))" order="ascending"/>
<xsl:if test="position() = 1">
<xsl:value-of select="CarFromDate"/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="arrays" >
<Item>
<xsl:value-of select="$Car"/>
</Item>
<Item>
<xsl:value-of select="$Hotel"/>
</Item>
<Item>
<xsl:value-of select="$Flight"/>
</Item>
</xsl:variable>
<xsl:param name="array" select="msxsl:node-set($arrays)"/>
<xsl:template match="/">
<xsl:for-each select="TRSummary">
<xsl:value-of select="$array/Item"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
<TRSummary>
24/04/2012 22/04/2015 29/04/2011
29/04/2011
如何获得最短日期。 请尽快指导我。
感谢和安培;此致 瑞木
<FlightList>
<TravelDate>28/04/2014</TravelDate>
</FlightList>
<FlightList>
<TravelDate>24/04/2012</TravelDate>
</FlightList>
<FlightList>
<TravelDate>30/04/2013</TravelDate>
</FlightList>
<HotelsList>
<CheckInDate>29/04/2015</CheckInDate>
</HotelsList>
<HotelsList>
<CheckInDate>22/04/2015</CheckInDate>
</HotelsList>
<HotelsList>
<CheckInDate>31/05/2016</CheckInDate>
</HotelsList>
<CarList>
<CarFromDate>29/04/2015</CarFromDate>
</CarList>
<CarList>
<CarFromDate>29/04/2011</CarFromDate>
</CarList>
<CarList>
<CarFromDate>23/04/2015</CarFromDate>
</CarList>
</TRSummary>
答案 0 :(得分:0)
假设您当前的输出只是29/04/2011
,而您的预期输出是24/04/2012 22/04/2015 29/04/2011
,那么问题就在于此代码
<xsl:for-each select="TRSummary">
<xsl:value-of select="$array/Item"/>
</xsl:for-each>
首先,您在xsl:for-each
上确实不需要TRSummary
,因为XML中只有一个这样的元素。但是,真正的问题是xsl:value-of
只会输出Item
变量中第一个array
的值。
你真的需要对项目进行xsl:for-each
,如此
<xsl:template match="/">
<xsl:for-each select="$array/Item">
<xsl:value-of select="." />
<xsl:text> </xsl:text>
</xsl:for-each>
</xsl:template>
另一方面,如果您只想要三个日期中的最小日期,您可以对这些项目进行另一种排序,例如
<xsl:template match="/">
<xsl:for-each select="$array/Item">
<xsl:sort select="number(substring(., 7, 4))" order="ascending"/>
<xsl:sort select="number(substring(., 4, 2))" order="ascending"/>
<xsl:sort select="number(substring(., 1, 2))" order="ascending"/>
<xsl:if test="position() = 1">
<xsl:value-of select="." />
</xsl:if>
</xsl:for-each>
</xsl:template>
如果是这种情况,您可以简化整个样式表,并取消对节点集的需求
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:for-each select="TRSummary/*">
<xsl:sort select="number(substring(*, 7, 4))" order="ascending"/>
<xsl:sort select="number(substring(*, 4, 2))" order="ascending"/>
<xsl:sort select="number(substring(*, 1, 2))" order="ascending"/>
<xsl:if test="position() = 1">
<xsl:value-of select="." />
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>