[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: Count words and add line-brake

Subject: RE: Count words and add line-brake
From: "Andrew Welch" <ajwelch@xxxxxxxxxxxxxxx>
Date: Mon, 8 Nov 2004 17:40:09 -0000
xsl count
> I hope someone can help. I would like to do the following.
>
> 1. Count the words in a specific xml node.
> 2. Insert a line-brake after every 15th word.
>
> Source:
> <txt>Here is some text. Here is some text. Here is some text.
> Here is some text. Here is some text.Here is some text. Here
> is some text. Here is some text. Here is some text. Here is
> some text.</txt>
>
>
> Output:
> <txt>
>      Here is some text. Here is some text. Here is some text.
> Here is some <br />
>      text. Here is some text. Here is some text. Here is some
> text. Here is <br />
>      some text. Here is some text. Here is some text.
> </txt>

I would suggest using XSLT 2.0 or XSLT 1.0 + tokenize extension function
to do this, then you write something like the following:

<xsl:template match="txt">
  <txt>
    <xsl:for-each select="tokenize(.,'\s')">
        <xsl:copy-of select="."/>
        <xsl:if test="position() mod 15 = 0"><br /></xsl:if>
    </xsl:for-each>
    ...


If you are using XSLT 1.0 + extensions then the tokenize funtion would
need be in the processors namespace (for example saxon:tokenize(...))

I'm sure there's a few gotchas with using the \s character class for
this task, but I (still) don't know regexs well enough to come upu with
anything better.

cheers
andrew

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.