[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: selecting nodes based on sibling values

Subject: RE: selecting nodes based on sibling values
From: "Fabien Tillier" <f.tillier@xxxxxxxx>
Date: Tue, 23 Nov 2010 16:26:44 +0100
RE:  selecting nodes based on sibling values
Thanks Wendell.
I have to compare timings with Martin solution...
But this one is amazing :)

Best regards,
Fabien


-----Message d'origine-----
De : Wendell Piez [mailto:wapiez@xxxxxxxxxxxxxxxx]
Envoyi : mardi 23 novembre 2010 16:21
@ : xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Objet : Re:  selecting nodes based on sibling values

Fabien,

At 09:45 AM 11/23/2010, you wrote:
>Let's say I have an XML like below...
>
><data>
>         <row>
>                 <level>40</level>
>                 <keycode>1254.45.12</keycode>
>         </row>
>         <row>
>                 <level>50</level>
>                 <keycode>1254.45.12.7</keycode>
>         </row>
>         <row>
>                 <level>50</level>
>                 <keycode>1254.45.12.8</keycode>
>         </row>
>         <row>
>                 <level>50</level>
>                 <keycode>1254.45.12.9</keycode>
>         </row>
>
>         <row>
>                 <level>40</level>
>                 <keycode>99.25.6</keycode>
>         </row>
>         <row>
>                 <level>50</level>
>                 <keycode>99.25.6.45</keycode>
>         </row>
>         <row>
>                 <level>50</level>
>                 <keycode>99.25.6.46</keycode>
>         </row>
></data>
>
>What could be the XPath expression to get the maximum number of nodes of
>level=50 in data those keycode starts like the level=40 line ?
>Here the answer would be 3 as the maximum number of level = 50 nodes for
>a given level = 40 is 3
>I have tried several things, but I am stuck....
>(I am using XSLT 2.0 on Saxon)

This is a bit tricky since associating the level 50 nodes with the
level 40 nodes is a bit tricky.

One way to do this is using a key (an old 1.0 upconversion trick):

<xsl:key name="get-level-50" match="row[level='50']"
   use="generate-id(preceding-sibling::row[level='40'][1])"/>

This enables you to get all the level 40 rows that belongs to any
level 50 row, using that row as context.

Then we can get a sequence representing the values of the counts like so:

//row[level='40']/count(key('get-level-50',generate-id(.))

and the max:

max(//row[level='40']/count(key('get-level-50',generate-id(.)))

Note: untested; watch out for typos.

Cheers,
Wendell



======================================================================
Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
   Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================

Current Thread

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-2013 All Rights Reserved.