Question

This XPath expression:

for $n in 1 to 5 return $n

Returns

1 2 3 4 5

Is it possible to do something similar with alphabetic characters?

Was it helpful?

Solution

Yep:

for $n in 65 to 70 return fn:codepoints-to-string($n)

returns:

A
B
C
D
E

In ascii/iso-8859-1 at least.

for $n in fn:string-to-codepoints('A') to fn:string-to-codepoints('E') 
    return fn:codepoints-to-string($n)

should work in any locale.

OTHER TIPS

Or, in XPath 3.0 (XSLT 3.0):

((32 to 127) ! codepoints-to-string(.))[matches(., '[A-Z]')]

Here we don't know whether or not the wanted characters have adjacent character codes (and in many real cases they wouldn't).

A complete XSLT 3.0 transformation using this XPath 3.0 expression:

<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

 <xsl:template match="/">
  <xsl:sequence select=
  "((32 to 127) ! codepoints-to-string(.))[matches(., '[A-Z]')]
  "/>
 </xsl:template>
</xsl:stylesheet>

When this transformation is applied (I am using Saxon-EE 9.4.0.6J) on any XML document (not used), the wanted, correct result is produced:

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

In case we know the wanted result characters have all-adjacent character codes, then:

(string-to-codepoints('A') to string-to-codepoints('Z')) ! codepoints-to-string(.)

Explanation:

Use of the new XPath 3.0 simple map operator !.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top