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

Re: How to write this XQuery program?

Subject: Re: How to write this XQuery program?
From: "cking" <cking@xxxxxxxxxx>
Date: Mon, 6 Sep 2004 22:42:24 +0200
xml compare
Hi Zhimao,

Here's an implementation of Michael Kay's "second approach":
> (b) sort the companies in order of price, and take the first

--- compare.xml ---
<compare>
 <p>p1</p>
 <p>p2</p>
 <c>c1.xml</c>
 <c>c2.xml</c>
 <c>c3.xml</c>
</compare>

Used as input file: you can specify any number of part id's and 
any number of 'company'-files.

The stylesheet will collect all the companies into one global variable 
with "document(/compare/c)/company", and then for each <p>,
generate a list sorted by price, outputting the first (therefore, it
needs the node-set extension).

--- compare.xsl ---
<xsl:stylesheet version="1.0" 
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:exsl="http://exslt.org/common"
 extension-element-prefixes="exsl"
 >

 <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

 <xsl:variable name="companies" select="document(/compare/c)/company"/>

 <xsl:template match="/compare">
  <result>
   <xsl:apply-templates select="p"/>
  </result>
 </xsl:template>

 <xsl:template match="p">
  <part>
   <id><xsl:value-of select="."/></id>
   <xsl:variable name="all">
    <xsl:for-each select="$companies/parts/part[id=current()]">
     <xsl:sort select="price" data-type="number"/>
     <price><xsl:value-of select="price"/></price>
     <company><xsl:value-of select="../../name"/></company>
    </xsl:for-each>
   </xsl:variable>
   <xsl:copy-of select="exsl:node-set($all)/price[1]"/>
   <xsl:copy-of select="exsl:node-set($all)/company[1]"/>
  </part>
 </xsl:template>

</xsl:stylesheet>

saxon output.xml compare.xml compare.xsl

--- output.xml ---
<result>
   <part>
      <id>p1</id>
      <price>100</price>
      <company>CompanyA</company>
   </part>
   <part>
      <id>p2</id>
      <price>100</price>
      <company>CompanyC</company>
   </part>
</result>

Cheers,
Anton Triest

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.