xsl-list
[Top] [All Lists]

RE: Count words and add line-brake

2004-11-08 10:40:09

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


<Prev in Thread] Current Thread [Next in Thread>