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="http://www.w3.org/1999/XSL/Transform"; version="2.0" xmlns:ati="test" xmlns:xs="http://www.w3.org/2001/XMLSchema ">
<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:choose>
<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)"/>
</xsl:when>
<xsl:otherwise>
<p><xsl:value-of select="$nodes[1]"/></p>
</xsl:otherwise>
</xsl:choose>
</xsl:function>
<xsl:template match="/">
<body>
<xsl:sequence select="ati:hachette.extract(body/descendant::p, 0)"/>
</body>
</xsl:template>
</xsl:stylesheet>


-- Jeff

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

I thought of following (a 2.0 solution).

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

<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>
 <xsl:if test="($x + $y) &lt; 1000">
   <xsl:apply-templates select="following-sibling::p[1]" />
 </xsl:if>
</xsl:template>

This is not tested.

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

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:
<book>
      <body>
              <p>some text</p>
              <p>some text</p>
              ...
              <p>some text, here is the 1,000th character, some more
text</p>
              <p>some text</p>
      </body>
</book>

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