Page 1 of 1

Find index of matches in xsl:analyze-string?

Posted: Fri Oct 04, 2013 8:02 pm
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!!!

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

Posted: Mon Oct 07, 2013 2:59 pm
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