how to show only two items in xslt?

2019-09-15 12:22发布

问题:

could yu please tell me how to show first two element is xslt ? here is my code 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>

expected output : 12

回答1:

If you only want to select the first two t elements which have the live attribute set to "2", you could put this logic in the xsl:apply-templates rather than the template match

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

Try this 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>