xsl-list
[Top] [All Lists]

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

2008-12-23 03:04:32
Hi Jeff,

If you're not interested in actually truncating the last paragraph, the stylesheet below should do the trick. See the comments for explanation. It will work for any sequence of elements within <body> (not just <p>).

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

 <xsl:template match="body">
   <!-- Process just the first child element -->
   <xsl:apply-templates mode="up-to-1000" select="*[1]"/>
 </xsl:template>

         <!-- Recursively process the child elements -->
         <xsl:template match="*" mode="up-to-1000">
           <xsl:param name="char-count" select="0"/>
           <!-- The running character count consists of the count so far
                plus the length of the string-value of this element. -->
<xsl:variable name="new-char-count" select="$char-count + string-length(.)"/>

           <!-- Copy this element -->
           <xsl:copy-of select="."/>

<!-- Only process (copy) the next one if we haven't reached 1000 yet -->
           <xsl:if test="$new-char-count &lt; 1000">
<xsl:apply-templates mode="up-to-1000" select="following-sibling::*[1]">
               <!-- Propagate our running total -->
               <xsl:with-param name="char-count" select="$new-char-count"/>
             </xsl:apply-templates>
           </xsl:if>
         </xsl:template>

</xsl:stylesheet>

Let me know if you have any questions about the above.

Evan


Jeff Sese 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


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--