如何在xslt中显示最后两个节点?

时间:2017-02-20 07:02:09

标签: jquery xml xslt xpath xslt-1.0

你能不能帮我在xslt中获取最后两个节点。这是我的代码 http://xsltransform.net/bwdwsJ/1

expectoutput

<h1>Preview your result as PDF when doctype is set to XML and your document starts with
      root element of XSL-FO. Apache FOP is used to generate the PDF
   </h1>
   <h1>Added some links to useful XSLT sites</h1>

xslt代码

 <xsl:template match="/">
      <hmtl>
        <head>
          <title>New Version!</title>
        </head>
        <xsl:for-each select="ul/li[last() &gt;2]">
            <h1><xsl:value-of select="."/></h1>
        </xsl:for-each>
      </hmtl>
    </xsl:template>

XML

<?xml version="1.0" encoding="UTF-8"?>

    <ul>
        <li>A new XSLT engine is added: Saxon 9.5 EE, with a license (thank you Michael Kay!)</li>
        <li>XSLT 3.0 support when using the new Saxon 9.5 EE engine!</li>
        <li>Preview your result as HTML when doctype is set to HTML (see this example)</li>
        <li>Preview your result as PDF when doctype is set to XML and your document starts with root element of XSL-FO. Apache FOP is used to generate the PDF</li>
        <li>Added some links to useful XSLT sites</li>
    </ul>

2 个答案:

答案 0 :(得分:0)

简短但有效:ul/li[position() &gt;= last()-1]

当前处理节点的位置必须大于或等于最后一个元素的索引减去1。

显示:http://xsltransform.net/bwdwsJ/2

答案 1 :(得分:0)

您可以使用以下XPath表达式:

/ul/li[position()>count(/ul/li)-2]