Thursday, 15 January 2015

xslt - catch the first occurance value -



xslt - catch the first occurance value -

i've below xml.

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

here i'm trying first occurrence of <page>.

i'm using below 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>

but output page valyes printed. want first one.

current output.

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

the page values printed here after <section class="tr_chapter">, want 22 i'm getting 2223 here i'm using //page[1]/text(), because i'm not sure page comes within section, random.

please allow me know how can first page value.

here transformation http://xsltransform.net/3nzcbsr

thanks

try:

<xsl:value-of select="(//page)[1]"/>

http://xsltransform.net/3nzcbsr/1

note gets value of first page element in entire document.

xslt xslt-2.0

No comments:

Post a Comment