我正在使用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进行排序,然后选择与价格最高的书籍相关联的作者.我尝试了各种各样的东西,但我似乎无法弄明白.
你可以指定一个< 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>