Character Access

1. Empty spaces turn to %20
2. Access to individual characters in a string
3. Counting characters
1.

Empty spaces turn to %20

Jeni Tennison



> This will get easier in XSLT 2.0, right?  Something like:
>
> <xsl:analyze-string select="text()" regex="%20">
>   <xsl:non-matching-substring>
>     <xsl:value-of select="."/>
>   </xsl:non-matching-substring>
>   <xsl:matching-substring>
>     <xsl:text> </xsl:text>
>   <xsl:matching-substring>
> </xsl:analyze-string>
>
> A bit unwieldy for a string replacement construct (unless I'm
> missing a shorthand way of doing this), but I'll take it,
> particularly when I have no control over the source XML.

Easier would be to use the replace() function in XPath 2.0:

  <xsl:value-of select="replace(text(), '%20', ' ')" />

You only need to use <xsl:analyze-string> if you want to do further manipulation of the matching (or non-matching) strings.

2.

Access to individual characters in a string

David Carlisle


> How do I access each char of copy99 ? copy99[1], copy99[2]... ?

substring($copy99,5,1) is the 5th character of  the string


            
3.

Counting characters

Michael Kay


> What I want to do is to count the number os characters in a  
> text node and all previous text nodes children of the current 
> text node's parent.

Well the XPath 2.0 solution is

sum(for $i in preceding-sibling::text() return string-length($i))

For XSLT 1.0 it's much more difficult, it's the classic problem of summing a calculated value over a node-set. There are several workable solutions:

- Construct a result tree fragment containing the computed values, then use the sum() and xx:node-set() functions to do the summation.
- Use a recursive template
- Use Dimitre's FXSL library.