How can I select the first element using XSLT?

后端 未结 4 1024
栀梦
栀梦 2021-02-03 21:38

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?



        
相关标签:
4条回答
  • 2021-02-03 21:42
    //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]!

    0 讨论(0)
  • 2021-02-03 21:44
    //newsItem[1]
    

    should do

    0 讨论(0)
  • 2021-02-03 21:50

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

    <xsl:for-each select="newsItem[1]">
      <div><xsl:value-of select="dateCreated"/></div>
      <div><xsl:value-of select="summary"/></div>
    </xsl:for-each>
    
    0 讨论(0)
  • 2021-02-03 22:03

    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>
    
    0 讨论(0)
提交回复
热议问题