2014-10-08 43 views
0

我有下面的XML赶上第一次出现的值

<chapter num="1"> 
<section level="sect2"> 
<page>22</page> 
</section> 
<section level="sect3"> 
<page>23</page> 
</section> 
</chapter> 

这里我试图得到第一个出现<page>

我正在使用下面的XSLT。

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ntw="Number2Word.uri" exclude-result-prefixes="ntw"> 
    <xsl:output method="html"/> 
    <xsl:strip-space elements="*"/> 


    <xsl:variable name="ThisDocument" select="document('')"/> 


    <xsl:template match="/"> 
     <xsl:text disable-output-escaping="yes"><![CDATA[<!DOCTYPE html>]]></xsl:text> 

     <html> 

      <body> 
      <xsl:apply-templates/> 
      </body> 
     </html> 
    </xsl:template> 

    <xsl:template match="chapter"> 
     <section class="tr_chapter"> 
     <xsl:value-of select="//page[1]/text()"/> 
      <div class="chapter"> 


      </div> 
     </section> 
    </xsl:template> 
</xsl:stylesheet> 

但输出,我得到所有的page valyes印。我只想要第一个。

电流输出。

<!DOCTYPE html> 
    <html> 
    <body> 
     <section class="tr_chapter">2223 
     <div class="chapter"> 
     </div> 
     </section> 
    </body> 
    </html> 

page值都在这里印刷<section class="tr_chapter">后,我想只有但我得到 这里我使用//page[1]/text(),因为我不知道该page来自内section,它是随机的。

请让我知道如何我只能得到第一page价值。

这里是改造http://xsltransform.net/3NzcBsR

感谢

+0

只使用'page [1]/text()' – 2014-10-08 10:39:14

+0

它不会打印任何东西@ JoelM.Lamsen,我用转换链接更新了我的问题。 – user3872094 2014-10-08 10:42:52

+0

oops,应该是'descendant :: page [1]/text()' – 2014-10-08 11:01:38

回答

1

如果你要搜索的chapter上下文元素的内容,在您的模板第一个page后代然后用<xsl:value-of select="descendant::page[1]"/><xsl:value-of select="(.//page)[1]"/>