2017-04-06 78 views
0

能不能請告訴我如何顯示前兩個元素是xslt? 這裏是我的代碼 http://xsltransform.net/ehVYZMp/6如何在xslt中只顯示兩個項目?

<?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

+0

您可以編輯您的問題以包含輸入XML嗎? xslttransform很棒,但它目前不可用....謝謝! –

回答

3

如果你只需要選擇其中有live屬性設置爲「2」,你可以把這個前兩個t元素在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> 
相關問題