Re: [xsl] Applying Templates Up To The Node That Contains The nth Descendant Character

Subject: Re: [xsl] Applying Templates Up To The Node That Contains The nth Descendant Character
From: Jeff Sese <jeferson.sese@xxxxxxxxxxxx>
Date: Tue, 23 Dec 2008 16:42:31 +0800
Thanks for the replies! I tried this, and it seems to work for my requirement:

<xsl:stylesheet xmlns:xsl=""; version="2.0" xmlns:ati="test" xmlns:xs=" ">
<xsl:function name="ati:hachette.extract" as="item()*">
<xsl:param name="nodes" as="node()*"/>
<xsl:param name="curLength"/>
<xsl:variable name="newLength" as="xs:double" select="$curLength + string-length($nodes[1])"/>
<xsl:when test="$newLength lt 500 and not(empty(remove($nodes, 1)))">
<p><xsl:value-of select="$nodes[1]"/></p>
<xsl:sequence select="ati:hachette.extract(remove($nodes, 1), $newLength)"/>
<p><xsl:value-of select="$nodes[1]"/></p>
<xsl:template match="/">
<xsl:sequence select="ati:hachette.extract(body/descendant::p, 0)"/>

-- Jeff

On 12 23, 08, at 4:23 PM, Mukul Gandhi wrote:

I thought of following (a 2.0 solution).

<xsl:template match="body">
   <xsl:apply-templates select="p[1]" />

<xsl:template match="p">
 <xsl:variable name="x"
select="string-length(string-join(preceding-sibling::p, ''))" />
 <xsl:variable name="y" select="string-length(.)" />
 <xsl:if test="$x &lt; 1000">
   <xsl:copy-of select="." />
 <xsl:if test="($x + $y) &lt; 1000">
   <xsl:apply-templates select="following-sibling::p[1]" />

This is not tested.

On Tue, Dec 23, 2008 at 11:58 AM, Jeff Sese <jeferson.sese@xxxxxxxxxxxx > wrote:

I have a book structure mark-up and I'm trying to get an extract of the book
contents to represent a preview. However, I only want to get the contents
upto the node that contains the nth descendant character of the book body.
How can I do this?

I have:
              <p>some text</p>
              <p>some text</p>
              <p>some text, here is the 1,000th character, some more
              <p>some text</p>

I want my output to be all the descendant::p of body but only upto the p
that contains the 1000th character.

Thanks in advance,

-- Jeff

-- Regards, Mukul Gandhi

Current Thread