[xquery-talk] XPath/XQuery equivalent of xsl:number?

G. Ken Holman gkholman at CraneSoftwrights.com
Thu Nov 17 11:11:35 PST 2011

Below is what I use in the classroom for Roman numerals for a limited 
set of numbers.

For your alpha numbers I would work with codepoints-to-string() ... 
but it isn't something I've already done.

I hope this helps.

. . . . . . . . . . . Ken

xquery version "1.0";

   A library to transform a number less than 4000 to a sequence of 
Roman digits.

   Crane Softwrights Ltd. XQuery Training

module namespace n2r = "urn:X-Crane:n2roman";

(:the basis of transformation is a series of strings for components:)
declare variable $n2r:values as element(value)+ :=
   <value num="1"    char="I" />,
   <value num="4"    char="IV"/>,
   <value num="5"    char="V" />,
   <value num="9"    char="IX"/>,
   <value num="10"   char="X" />,
   <value num="40"   char="XL"/>,
   <value num="50"   char="L" />,
   <value num="90"   char="XC"/>,
   <value num="100"  char="C" />,
   <value num="400"  char="CD"/>,
   <value num="500"  char="D" />,
   <value num="900"  char="CM"/>,
   <value num="1000" char="M" />

(:return the concatenation of strings by continuous reduction:)
declare function n2r:n2roman ( $num as xs:integer ) as xs:string
   (:as long as we have a number, keep going:)
   if ( $num ) then
     (:reduce by the largest number that has a string value:)

     for $val in $n2r:values[@num <= $num][fn:last()] return
       (:using the highest value:)
       fn:concat( $val/@char,n2r:n2roman( $num - xs:integer( $val/@num ) ) )
   (:nothing left:)
   else ""

(:end of file:)

At 2011-11-17 14:04 -0500, Joe Wicentowski wrote:
>Hi all,
>I am constructing a table of contents based on a TEI document (whose
>structure consists primarily of nested divs), and would like to be
>able to generate appropriate heading levels for each the table of
>contents hierarchy.  Specifically, I want to be able to generate the
>"I", "II", "A", "B", and "a" bits in:
>I. Chapter 1
>II. Chapter 2
>   A. Section 1
>   B. Section 2
>     a) Subsection 1
>Put another way, borrowing from CSS terminology, I need to be able to
>format an integer as "upper-roman", "upper-alpha", and "lower-alpha."
>Borrowing from XSL terminology, I need to perform the equivalent
>function of <xsl:number>, as in:
>   <xsl:number value="2" format="I'/> ==> returns "II"
>   <xsl:number value="2" format="a'/> ==> returns "b"
>   <xsl:number value="2" format="A'/> ==> returns "B"
>I think what I really need is XPath 3.0's fn:format-integer()
>function[1], but it appears that XPath 3.0 is still at the Working
>Draft stage.
>So my question: Of course I could hack my own format-integer()
>function together (indeed, my current kludge is to pipe out my
>integers to an XSL stylesheet), but is there an XPath or XQuery
>library that would let me do this?
>[1] http://www.w3.org/TR/xpath-functions-30/#func-format-integer

Contact us for world-wide XML consulting and instructor-led training
Free 5-hour video lecture: XSLT/XPath 1.0 & 2.0 http://ude.my/t37DVX
Crane Softwrights Ltd.            http://www.CraneSoftwrights.com/q/
G. Ken Holman                   mailto:gkholman at CraneSoftwrights.com
Google+ profile: https://plus.google.com/116832879756988317389/about
Legal business disclaimers:    http://www.CraneSoftwrights.com/legal

More information about the talk mailing list