XSLT 1.0: max value of a date node

别来无恙 提交于 2019-12-11 13:11:59

问题


Given following xml:

<Ergebnisse>
    <Spiel>
        <Datum>2013-10-02</Datum>
    </Spiel>
    <Spiel>
        <Datum>2013-10-03</Datum>
    </Spiel>
    <Spiel>
        <Datum>2013-10-03</Datum>
    </Spiel>
    <Spiel>
        <Datum>2013-10-03</Datum>
    </Spiel>
    <Spiel>
        <Datum>2013-10-06</Datum>
    </Spiel>
    <Spiel>
        <Datum>2013-10-06</Datum>
    </Spiel>
    <Spiel>
        <Datum>2013-10-06</Datum>
    </Spiel>
    <Spiel>
        <Datum>2013-10-06</Datum>
    </Spiel>
    <Spiel>
        <Datum>2014-05-01</Datum>
    </Spiel>
    <Spiel>
        <Datum>2014-05-01</Datum>
    </Spiel>
    <Spiel>
        <Datum>2014-04-27</Datum>
    </Spiel>
</Ergebnisse>

Now I need to know, which is the highest date-value in "Datum". I'm using Python and lxml, so I can only work with Xpath 1.0. I tried:

//Spiel[not (Datum < preceding::Spiel/Datum) and not (Datum < following::Spiel/Datum)]/Datum

but it returns all values. What can I do? Thanks!


回答1:


In XSLT 1.0 you can sort the values and take the last.

<xsl:for-each select="Speil/Datum">
  <xsl:sort select="."/>
  <xsl:if test="position()=last()"><xsl:value-of select="."/></xsl:if>
</xsl:for-each>

I wouldn't attempt it in XPath alone - if XPath 1.0 is all you have, return all the values and do the extraction in the Python host language.



来源:https://stackoverflow.com/questions/19133038/xslt-1-0-max-value-of-a-date-node

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