Find index of matches in xsl:analyze-string?

Here should go questions about transforming XML with XSLT and FOP.
feickertmd
Posts: 12
Joined: Tue Nov 23, 2010 8:00 pm

Find index of matches in xsl:analyze-string?

Post by feickertmd »

I am using xsl:analyze-stirng to prse a long string and break it into separate lines that are under a given length.
Example input

Code: Select all


<root>
<string>210 character string</string>
</root>
I already have working code that gives me this:

Code: Select all


<root>
<string>50 character string</string>
<string>50 character string</string>
<string>50 character string</string>
<string>50 character string</string>
<string>10 character string</string>
</root>
... where the 10 character string is the result of xsl:nonmatching-substring.

What I need to get is this:

Code: Select all


<root>
<string position="1">50 character string</string>
<string position="2">50 character string</string>
<string position="3">50 character string</string>
<string position="4">50 character string</string>
<string position="5">10 character string</string>
</root>
...where the position value corresponds to the index of the substring (i.e., the first matching substring is 1, the next is 2, and so on).

Need this soon, so ideas appreciated!!!
adrian
Posts: 2881
Joined: Tue May 17, 2005 4:01 pm

Re: Find index of matches in xsl:analyze-string?

Post by adrian »

Hi,

A simple idea is to collect all of them in a node-set variable and then iterate on the nodes from that for the position.
e.g.

Code: Select all

<xsl:variable name="results">
<xsl:analyze-string select="text()" regex=".+">
<xsl:matching-substring>
<res><xsl:value-of select="."/></res>
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:variable>
<xsl:for-each select="$results/*">
<string position="{position()}"><xsl:value-of select="string-length(.)"/> character string</string>
</xsl:for-each>
The variable collects the actual matching substrings, but you can of course collect the already processed results (the string length).

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
Post Reply