xml – 如何排序,然后选择一个项目
发布时间:2020-12-15 23:54:31 所属栏目:百科 来源:网络整理
导读:我正在使用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/Tra
我正在使用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> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |