xml - In XSL, how can I print only the first line in for-each block? -
i made list of books in xml. following format of xml file; there more <book>
blocks, of course.
<data> <book> <title>encyclopaedia britannica</title> <category>encyclopedia</category> <language>english</language> <author>encyclopaedia britannica editorial</author> <year>1768</year> <price>49.99</price> </book> </data>
and want print title of expensive one. tried follows in .xsl
file:
<p style = "display: block;"> expensive book " <xsl:for-each select="data/book"> <xsl:sort select="price" order="descending"/> <!-- <xsl:value-of select="title"/> --> </xsl:for-each> <xsl:value-of select="data/book/title"/> " </p>
when <xsl:value-of>
block in <xsl:for-each>
(which set comment) executed prints books sorted in descending order. of course, <xsl:value-of select="data/book/title"/>
makes first book of original table printed.
so want print expensive 1 stopping printing other books first 1 when table sorted.
i know there's no such thing break
, thought using <xsl:if>
block check if row first or not. idea possible? or can in way?
so thought using
<xsl:if>
block check if row first or not.
yes, correct idea:
<xsl:for-each select="/data/book"> <xsl:sort select="price" data-type="number" order="descending"/> <xsl:if test="position()=1"> <xsl:value-of select="title"/> </xsl:if> </xsl:for-each>
note selects first expensive title in case of tie.
or can in way?
that depends on xslt processor use. in xslt 2.0, can use max()
function. , several xslt 1.0 processors support math:max()
, math:highest()
extension functions.
Comments
Post a Comment