微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

xml – 如何排序,然后选择一个项目

我正在使用XSLT从Feed获取数据.目前我使用这个 block of code,它只是从Feed中选择第一个项目.我稍微改了一下,所以它适用于这个示例XML.
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
  <html>
  <body>
  <xsl:apply-templates/> 
  </body>
  </html>
</xsl:template>

<xsl:template match="/">
  <xsl:value-of select="catalog/book/author"/>
</xsl:template>

</xsl:stylesheet>

我想按价格对xml进行排序,然后选择与价格最高的书籍相关联的作者.我尝试了各种各样的东西,但我似乎无法弄明白.

目前的输出是“Gambardella,Matthew”,但我需要它是“galos,Mike”.

你可以指定一个< xsl:sort>在apply-templates中,如下所示:
<xsl:template match="/">
    <html>
        <body>
            <xsl:apply-templates select="/catalog/book">
                <xsl:sort select="price" order="descending" data-type="number"/>
            </xsl:apply-templates>
        </body>
   </html>
</xsl:template>

然后在你的小’book’模板中,使用position()过滤掉第一本书节点

<xsl:template match="book">
    <xsl:if test="position() = 1">
        <xsl:value-of select="author"/>
        <br/>
        <xsl:value-of select="price"/>
    </xsl:if>
</xsl:template>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。