Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I select the first element using XSLT?

Tags:

html

xml

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?

<xml>     <news>         <newsitem>             <dateCreated>2009-09-09</dateCreated>             <summary>Something great happened</sumamry>         </newsitem>         <newsitem>             <dateCreated>2009-09-08</dateCreated>             <summary>Something bad happened</sumamry>         </newsitem>         <newsitem>             <dateCreated>2009-09-07</dateCreated>             <summary>Something really bad happened</sumamry>         </newsitem>     </news> </xml> 
like image 617
John B Avatar asked Sep 30 '09 15:09

John B


People also ask

How do I select a substring in XSLT?

XSLT doesn't have any new function to search Strings in a reverse manner. We have substring function which creates two fields substring-before-last and substring-after-last.In XSLT it is defined as follows: <xsl:value-of select="substring (string name ,0, MAX_LENGTH )"/>...

What does 1 mean in XSLT?

This answer is not useful. Show activity on this post. The expression: select="RCODE[1]" means select the first RCODE child.

Which symbol is used to get the element value in XSLT?

XSLT <xsl:value-of> Element The <xsl:value-of> element is used to extract the value of a selected node.


1 Answers

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

<?xml version="1.0"?> <xsl:transform version="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:output mode="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:template match="//newsItem[1]">     <div><xsl:value-of select="dateCreated"/></div>     <div><xsl:value-of select="summary"/></div> </xsl:template>  </xsl:transform> 
like image 159
brianary Avatar answered Oct 10 '22 08:10

brianary