how to show only two items in xslt?

流过昼夜 提交于 2019-12-12 03:40:56

问题


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>


来源:https://stackoverflow.com/questions/43253447/how-to-show-only-two-items-in-xslt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!