如何在 xslt 中只显示两个项目?

how to show only two items in xslt?

你能告诉我如何显示前两个元素是 xslt 吗? 这是我的代码 http://xsltransform.net/ehVYZMp/6

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:exsl="http://exslt.org/common"
 extension-element-prefixes="exsl">
    <xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />

    <xsl:template match="a">

      <hmtl>
        <head>
          <title>New Version!</title>
        </head>
          <xsl:apply-templates select="t"/>
      </hmtl>
    </xsl:template>

<xsl:template match="t[@live='2']">
<xsl:value-of select="@b"/>
 </xsl:template>
</xsl:transform>

预期输出:12

如果您只想 select 前两个 t 元素 live 属性设置为“2”,您可以将此逻辑放在 xsl:apply-templates 而不是模板匹配

<xsl:apply-templates select="t[@live='2'][position() &lt; = 2]"/>

试试这个 XSLT

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:exsl="http://exslt.org/common"
 extension-element-prefixes="exsl">
    <xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />

    <xsl:template match="a">

      <hmtl>
        <head>
          <title>New Version!</title>
        </head>
          <xsl:apply-templates select="t[@live='2'][position() &lt; = 2]"/>
      </hmtl>
    </xsl:template>

<xsl:template match="t">
<xsl:value-of select="@b"/>
 </xsl:template>
</xsl:transform>