Altova Mailing List Archives>Archive Index >microsoft.public.xml Archive Home >Recent entries >Thread Prev - Re: Complicated X Query [Thread Next] Re: Complicated X QueryTo: NULL Date: 5/4/2007 11:32:00 AM Hi, Dimitre's XSLT could be translated into XQuery as follows: let $ord := (for $ord in doc("temp.xml")/*/*/order order by max(for $nItems in count($ord/item), $i in 1 to $nItems, $j in remove(1 to $nItems, $i) return($ord/item[$i]/@quantity + $ord/item[$j]/@quantity) ) descending return $ord)[1] return (for $item in $ord/item order by $item/quantity descending return $item)[position() le 2] Hope that helps, Priscilla ------------------------------------------------------------------------- Priscilla Walmsley Author, XQuery (O'Reilly Media) http://www.xqueryfunctions.com http://www.xsltfunctions.com http://www.datypic.com ------------------------------------------------------------------------- "Bhavin" <v2desperado@g...> wrote in message news:1178138211.275598.326500@h...... > On May 2, 1:08 am, "Dimitre Novatchev" <dimit...@tpg.com.au> wrote: >> Using XSLT 2.0 one possible solution is the following: >> >> <xsl:stylesheet version="2.0" >> xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >> > >> <xsl:output omit-xml-declaration="yes" indent="yes"/> >> <xsl:template match="/"> >> >> <xsl:for-each select="/*/*/order"> >> <xsl:sort data-type="number" order="descending" select= >> "max(for $nItems in count(item), >> $i in 1 to $nItems, >> $j in remove(1 to $nItems, $i) >> return(item[$i]/@quantity >> + >> item[$j]/@quantity) >> ) >> " >> /> >> >> <xsl:if test="position() = 1"> >> <xsl:for-each select="item"> >> <xsl:sort order="descending" select="quantity"/> >> >> <xsl:if test="position() le 2"> >> <xsl:sequence select="."/> >> </xsl:if> >> </xsl:for-each> >> </xsl:if> >> </xsl:for-each> >> >> </xsl:template> >> </xsl:stylesheet> >> >> When applied on the provided source xml document, the result is: >> >> <item product-id="msft30" quantity="6"/> >> <item product-id="sam2" quantity="2"/> >> >> Cheers, >> Dimitre Novatchev >> >> "Bhavin" <v2desper...@gmail.com> wrote in message >> >> news:1178076287.582665.124210@y...... >> >> >I need to write a complicated X query. The Query is >> > 1] which lists 2 most popular pairs of products bought together. >> >> > and XML file is >> >> > <e-store> >> > <product id="cre30" name="Creative ZEN VISION" >> > price="255.99">Creative White 30GB USB 2.0 ZEN VISION Model >> > 70PF115100011 - Retail</product> >> > <product id="ipod30" name="Apple iPod" price="236.99">Apple new >> > iPod video Black 30GB Model MA446LL/A - Retail</product> >> > <product id="sam2" name="Samsung Black" price="154.99"> SAMSUNG >> > Black 2GB USB 2.0 Audio Player with Built-in Speakers Model YP-K5JQB - >> > Retail</product> >> > <product id="tosh60" name="Toshiba Piano Black" price="175.99"> >> > Recertified: TOSHIBA Piano Black 60GB USB 2.0 Portable Media Player >> > Model MES60VK</product> >> > <product id="san4" name="SanDisk Sansa" price="135.99"> SanDisk >> > Sansa e260 Black 4GB USB 2.0 MP3 Player Model SDMX44096A70 - Retail</ >> > product> >> > <product id="msft30" name="Microsoft Zune Black" price="223.99"> >> > Microsoft Zune Black 30GB Hi-Speed USB, Wi-Fi(802.11 b/g) Large color >> > screen Digital media player Model JS8-00001 - Retail</product> >> > <product id="iaud2" name="iAUDIO Black" price="162.99">iAUDIO Black >> > 2GB USB 2.0 Portable Video Player Model D2-2048BL - Retail</product> >> > <product id="arch30" name="ARCHOS Silver" price="255.99"> ARCHOS >> > Silver 30GB USB 2.0 High-Speed Device (compatible USB 1.1): Mass >> > Storage class (MSC) and Media Transport Protocol (MTP)</product> >> > <product id="ipod80" name="Apple iPod video" price="324.99"> Apple >> > new iPod video Black 80GB Model MA450LL/A - Retail</product> >> > <product id="ipod2" name="Apple new iPod nano" price="142.99">Apple >> > new iPod nano Silver 2GB Model MA477LL/A - Retail</product> >> >> > <warehouse address="NY"> >> > <product-item product-id="cre30" quantity="5"/> >> > <product-item product-id="ipod30" quantity="3"/> >> > <product-item product-id="sam2" quantity="3"/> >> > <product-item product-id="tosh60" quantity="2"/> >> > <product-item product-id="san4" quantity="2"/> >> > </warehouse> >> >> > <warehouse address="TX"> >> > <product-item product-id="cre30" quantity="5"/> >> > <product-item product-id="sam2" quantity="3"/> >> > <product-item product-id="san4" quantity="20"/> >> > <product-item product-id="msft30" quantity="1"/> >> > <product-item product-id="iaud2" quantity="2"/> >> > <product-item product-id="ipod2" quantity="4"/> >> > </warehouse> >> >> > <client name="John" address="Buffalo, NY"> >> > <order order-date="2007-04-15" shipping-date="2007-04-18"> >> > <item product-id="cre30" quantity="1"/> >> > <item product-id="sam2" quantity="2"/> >> > <item product-id="arch30" quantity="1"/> >> > <item product-id="ipod80" quantity="4"/> >> > </order> >> > <order order-date="2007-04-15" shipping-date="2007-04-17"> >> > <item product-id="cre30" quantity="1"/> >> > <item product-id="ipod2" quantity="2"/> >> > <item product-id="sam2" quantity="2"/> >> > </order> >> > <order order-date="2007-04-23"> >> > <item product-id="msft30" quantity="6"/> >> > <item product-id="sam2" quantity="2"/> >> > <item product-id="ipod30" quantity="2"/> >> > </order> >> > </client> >> >> > <client name="Larry" address="Albany, NY"> >> > <order order-date="2007-04-18" shipping-date="2007-04-20"> >> > <item product-id="cre30" quantity="1"/> >> > <item product-id="ipod2" quantity="1"/> >> > <item product-id="sam2" quantity="1"/> >> > </order> >> > <order order-date="2007-04-18" shipping-date="2007-04-20"> >> > <item product-id="sam2" quantity="1"/> >> > <item product-id="ipod30" quantity="1"/> >> > <item product-id="san4" quantity="1"/> >> > </order> >> > <order order-date="2007-04-22"> >> > <item product-id="san4" quantity="1"/> >> > <item product-id="sam2" quantity="2"/> >> > <item product-id="iaud2" quantity="2"/> >> > </order> >> > </client> >> >> > <client name="Liz" address="Niagara Falls, NY"> >> > <order order-date="2007-03-12" shipping-date="2007-03-14"> >> > <item product-id="san4" quantity="1"/> >> > <item product-id="sam2" quantity="1"/> >> > <item product-id="arch30" quantity="3"/> >> > </order> >> > <order order-date="2007-04-24"> >> > <item product-id="iaud2" quantity="1"/> >> > </order> >> > </client> >> > </e-store> >> >> > Actually I am new to X query and it is hard to implement using X Path. >> > Any help will be really appreciated . >> >> > Regards > > Thank you very much i just want to say that I wanted in X query, it is > perfectly working in XSLT, But I want O/P in Xquery, because it is my > task to implement > I appreciate your efforts > > Regards > | ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
