[XQuery Talk Mailing List Archive Home] [By Date] [By Thread] [By Subject] [By Author] [Recent Entries] [Reply To This Message]

XPath/XQuery equivalent of xsl:number?

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


  XPath/XQuery equivalent of xsl:number?
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?
>
>Thanks,
>Joe
>
>[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:http://x-query.com/mailman/listinfo/talk
Google+ profile: https://plus.google.com/116832879756988317389/about
Legal business disclaimers:    http://www.CraneSoftwrights.com/legal



PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2011 All Rights Reserved.