Skip to content Skip to sidebar Skip to footer

How Can I Select The First Element Using Xslt?

I have a list of news items, sorted by dateCreated. I have a preview box control where I only want to show the first item. How can I do that using XSLT?

Solution 1:

If you wish to output XHTML 1.1, here's one way:

<?xml version="1.0"?><xsl:transformversion="2.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"xmlns:xs="http://www.w3.org/2001/XMLSchema"exclude-result-prefixes="xsl xs"><xsl:outputmode="xhtml"version="1.1"omit-xml-declaration="yes"encoding="utf-8"media-type="application/xhtml+xml"indent="no"doctype-public="-//W3C//DTD XHTML 1.1//EN"doctype-system="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd" /><xsl:templatematch="//newsItem[1]"><div><xsl:value-ofselect="dateCreated"/></div><div><xsl:value-ofselect="summary"/></div></xsl:template></xsl:transform>

Solution 2:

//newsItem[1]

should do

Solution 3:

I had the same question and I think I found a better answer:

<xsl:for-eachselect="newsItem[1]"><div><xsl:value-ofselect="dateCreated"/></div><div><xsl:value-ofselect="summary"/></div></xsl:for-each>

Solution 4:

//newsItem[1]

Selects the first book newsItem element, but note that IE5 and later has implemented that [0] should be the first node, but according to the W3C standard it should be [1]!

Post a Comment for "How Can I Select The First Element Using Xslt?"