需要一些帮助来排序,然后在XSLT中选择最大值或最小值
源xml:<target>
<relatedTarget>
<permitExpiry>2005-07-02T08:11:00.000Z</permitExpiry>
<permitStart>2015-07-11T09:22:00.000Z</permitStart>
</relatedTarget>
<relatedTarget>
<permitExpiry>2003-07-12T08:11:00.000Z</permitExpiry>
<permitStart>2014-07-01T09:22:00.000Z</permitStart>
</relatedTarget>
<relatedTarget>
<permitExpiry>2002-07-10T08:11:00.000Z</permitExpiry>
<permitStart>2016-07-06T09:22:00.000Z</permitStart>
</relatedTarget>
</target>
结果xml:
<target>
<relatedTarget>
<permitStart>2014-07-01T09:22:00.000Z</permitStart>
<permitExpiry>2005-07-02T08:11:00.000Z</permitExpiry>
</relatedTarget>
</target>
基本上我需要的结果应该有最小的permitStart日期和最大的permitExpiry日期从所有的日期到来。
我的示例xsl:
<xsl:template match="/">
<xsl:variable name="permitStartVar" select="//permitStart"/>
<xsl:variable name="permitStopVar" select="//permitExpiry"/>
<xsl:for-each select="relatedTask">
<xsl:sort select="substring(permitStart,1,4)" /> <!-- Year -->
<xsl:sort select="substring(permitStart,6,2)" /> <!-- Month -->
<xsl:sort select="substring(permitStart,9,2)" /> <!-- Day -->
<xsl:sort select="substring(permitStart,12,2)" /> <!-- Hour -->
<xsl:sort select="substring(permitStart,15,2)" /> <!-- Minute -->
<xsl:sort select="substring(permitStart,18,2)" /> <!-- Second -->
<xsl:sort select="substring(permitExpiry,1,4)" /> <!-- Year -->
<xsl:sort select="substring(permitExpiry,6,2)" /> <!-- Month -->
<xsl:sort select="substring(permitExpiry,9,2)" /> <!-- Day -->
<xsl:sort select="substring(permitExpiry,12,2)" /> <!-- Hour -->
<xsl:sort select="substring(permitExpiry,15,2)" /> <!-- Minute -->
<xsl:sort select="substring(permitExpiry,18,2)" /> <!-- Second -->
</xsl:for-each>
<target>
<relatedTarget>
<permitStart><xsl:value-of select="$permitStartVar[1]"/></permitStart>
<permitExpiry><xsl:value-of select="$permitStopVar[last()]"/></permitExpiry>
</relatedTarget>
</target>
</template>
您可以通过在xs:dateTime
值上使用max()
和min()
而不进行排序。
XML输入
<target>
<relatedTarget>
<permitExpiry>2005-07-02T08:11:00.000Z</permitExpiry>
<permitStart>2015-07-11T09:22:00.000Z</permitStart>
</relatedTarget>
<relatedTarget>
<permitExpiry>2003-07-12T08:11:00.000Z</permitExpiry>
<permitStart>2014-07-01T09:22:00.000Z</permitStart>
</relatedTarget>
<relatedTarget>
<permitExpiry>2002-07-10T08:11:00.000Z</permitExpiry>
<permitStart>2016-07-06T09:22:00.000Z</permitStart>
</relatedTarget>
</target>
XSLT 2.0 <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/*">
<target>
<relatedTarget>
<xsl:copy-of select="(relatedTarget/permitStart[.=min(/*/relatedTarget/xs:dateTime(permitStart))])[1]"/>
<xsl:copy-of select="(relatedTarget/permitExpiry[.=max(/*/relatedTarget/xs:dateTime(permitExpiry))])[1]"/>
</relatedTarget>
</target>
</xsl:template>
</xsl:stylesheet>
<target>
<relatedTarget>
<permitStart>2014-07-01T09:22:00.000Z</permitStart>
<permitExpiry>2005-07-02T08:11:00.000Z</permitExpiry>
</relatedTarget>
</target>
当我遵循你的方法时,
<xsl:stylesheet exclude-result-prefixes="xs" version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:template match="target">
<xsl:variable name="permitStartVar" select="//permitStart"/>
<xsl:variable name="permitStopVar" select="//permitExpiry"/>
<xsl:variable name="temp">
<xsl:for-each select="relatedTarget/permitStart">
<xsl:sort select="substring(permitStart,1,4)"/>
<!--Year-->
<xsl:sort select="substring(permitStart,6,2)"/>
<!--Month-->
<xsl:sort select="substring(permitStart,9,2)"/>
<!--Day-->
<xsl:sort select="substring(permitStart,12,2)"/> <!--Hour-->
<xsl:sort select="substring(permitStart,15,2)"/>
<!--Minute-->
<xsl:sort select="substring(permitStart,18,2)"/>
<!--Second-->
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="temp1">
<xsl:for-each select="relatedTarget/permitExpiry">
<xsl:sort select="substring(permitExpiry,1,4)" order="descending"/>
<!--Year-->
<xsl:sort select="substring(permitExpiry,6,2)" order="descending"/>
<!--Month-->
<xsl:sort select="substring(permitExpiry,9,2)" order="descending"/>
<!--Day-->
<xsl:sort select="substring(permitExpiry,12,2)" order="descending"/>
<!--Hour-->
<xsl:sort select="substring(permitExpiry,15,2)" order="descending"/>
<!--Minute-->
<xsl:sort select="substring(permitExpiry,18,2)" order="descending"/>
<!--Second-->
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:variable>
<target>
<relatedTarget>
<permitStart>
<xsl:value-of select="$temp//permitStart[1]"/>
</permitStart>
<permitExpiry>
<xsl:value-of select="$temp1/permitExpiry[1]"/>
</permitExpiry>
</relatedTarget>
</target>
</xsl:template>
</xsl:stylesheet>
输出:
<target>
<relatedTarget>
<permitStart>2014-07-01T09:22:00.000Z</permitStart>
<permitExpiry>2003-07-12T08:11:00.000Z</permitExpiry>
</relatedTarget>
</target>
编辑:
简化版:
<xsl:stylesheet exclude-result-prefixes="xs" version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:template match="target">
<xsl:variable name="permitStartVar" select="//permitStart"/>
<xsl:variable name="permitStopVar" select="//permitExpiry"/>
<xsl:variable name="temp">
<xsl:for-each select="relatedTarget/permitStart">
<xsl:sort select="."/>
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="temp1">
<xsl:for-each select="relatedTarget/permitExpiry">
<xsl:sort select="." order="descending"/>
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:variable>
<target>
<relatedTarget>
<permitStart>
<xsl:value-of select="$temp//permitStart[1]"/>
</permitStart>
<permitExpiry>
<xsl:value-of select="$temp1/permitExpiry[1]"/>
</permitExpiry>
</relatedTarget>
</target>
</xsl:template>
</xsl:stylesheet>