Project

Profile

Help

Counting Nodes in Sorted Lists

Added by Anonymous over 19 years ago

Legacy ID: #3103805 Legacy Poster: Gadrin (gadrin)

I'm pretty new to XQuery, and I'm using Saxon 8.3. So far so good. I have the following XQuery... let $Corvus := "C:/Program%20Files/RPC/Charac­ters/ThisCharacter.rpc" let $cnt := 0 for $sc in fn:distinct-values(fn:doc($Cor­vus)/Character/Headings/Headin­g/@Category) (: build a list of categories :) let $n := count(fn:distinct-values(fn:do­c($Corvus)/Character/Headings/­Heading[@Category=$sc]/@Name)) (: count them :) order by $sc return <div> <b>{$sc}: [{$n}]</b> { (: loop thru the entries for this category :) for $sp at $pos in fn:distinct-values(fn:doc($Cor­vus)/Character/Headings/Headin­g[@Category=$sc]/@Name) let $com := ", " order by $sp (: sort them :) return <span>{$pos}{$com}</span> } </div> (: ------------------------------­------------------------------­------------------------------­--- :) and along with the order by clause produces this output...which is sorted... (the numbers are the position) Category 1: [6 Total] 1, 4, 2, 3, 5, 6, Cateogry 2: [15 Total] 1, 9, 2, 13, 14, 3, 4, 11, 10, 5, 6, 7, 15, 8, 12, and is fine except for the comma's on the last item. how do I: 1) determine the last item in the SORTED LIST (vs the last position) ? 2) or if that's impossible, how do you count the items as you display them onscreen ? Frex: in Category 2 above, I need to know that $pos 12 is the last item in the sorted list. I'm so used to doing: count = count + 1


Replies (2)

RE: Counting Nodes in Sorted Lists - Added by Anonymous about 19 years ago

Legacy ID: #3104042 Legacy Poster: Michael Kay (mhkay)

I'll start with a couple of observations on your query. (a) The argument to fn:doc() is supposed to be a URI, not a filename. The Java URI library apparently allows you to get away with using a filename of the form c:/x/y, but it's not good practice. It should be "file:///c:/x/y". (b) Saxon doesn't (yet) recognize common subexpressions, so it would be a good idea to assign the value of fn:doc($Cor­vus)/Character/Headings/Headin­g to a variable. It's unfortunate that a for loop in XQuery doesn't allow you to use position() and last(), which would be the standard way of tackling this in XSLT. If it weren't for the <span> elements, you could achieve the required effect using string-join(). As it is, I think you have to assign the sorted sequence to a variable, use count() to see how many items it has, and then iterate over the sorted sequence using for $x at $p in $sorted-sequence return <span>{$x}{if $p ne $count($sorted-sequence) then $comma else ""}</span> Michael Kay

RE: Counting Nodes in Sorted Lists - Added by Anonymous about 19 years ago

Legacy ID: #3104352 Legacy Poster: Gadrin (gadrin)

a) yeah, well I don't have a web server to work with at the moment. b) if by subexpressions you mean @attributes, then yes. I kept punching the code until I found the distinct-values would pull them out correctly. I wanted something that could pull out values similar to the way ADO recordsets are persisted to XML. c) interesting, I'll give it a try, thanks.

    (1-2/2)

    Please register to reply