Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
|
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 15th, 2006, 03:52 AM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 131
Thanks: 10
Thanked 0 Times in 0 Posts
Default Return Largest Number

Hi,

I am trying to return the largest number of 'Sets' for each 'Test' in the XML example below, using my XSL schema.

In the edited example, the value returned should be '3', as there are 3 Sets for Test 0900 and only 2 Sets for Test 0901. How do I return the largest value '3' using XSL?

Can anyone help please?

XML
Code:
<Test Code="0900">
 <UOM>%</UOM>
 <Desc>NIR Moisture</Desc>
 <Set No="1">
  <Targ/>
  <Min/>
  <Max/>
 </Set>
 <Set No="2">
  <Targ/>
  <Min/>
  <Max/>
 </Set>
 <Set No="3">
  <Targ/>
  <Min/>
  <Max/>
 </Set>
</Test>
<Test Code="0901">
 <UOM>%</UOM>
 <Desc>NIR Oil</Desc>
 <Set No="1">
  <Targ/>
  <Min/>
  <Max/>
 </Set>
 <Set No="2">
  <Targ/>
  <Min/>
  <Max/>
 </Set>
</Test>
XSL
Code:
<xsl:template name="SampleDetails">
 <xsl:element name="NumberOfTestsRequired">
  <xsl:for-each select="*//Test">
   <xsl:for-each select=".//Set">

    <xsl:if test="position() =last()">    
     <xsl:value-of select="."/>
    </xsl:if>
   </xsl:for-each>
  </xsl:for-each>
 </xsl:element>
</xsl:template>
Many thanks,


Neal

A Northern Soul
__________________
Neal

A Northern Soul
 
Old September 15th, 2006, 04:18 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

In 2.0:

max(for $x in Test return count($x/Set))

In 1.0:

<xsl:variable name="max">
  <xsl:for-each select="Test">
     <xsl:sort select="count(Set)" data-type="number"/>
     <xsl:if test="position()=last()">
         <xsl:value-of select="count(Set)"/>

In both cases this assumes the parent of the Test elements is the context node.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old September 15th, 2006, 04:48 AM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 131
Thanks: 10
Thanked 0 Times in 0 Posts
Default

Many thanks Mike,

Excellent response for both versions (I needed 1.0).

The actual XSL code I used was:
Code:
<xsl:template name="SampleDetails">
 <xsl:element name="NumberOfTestsRequired">
  <xsl:variable name="max"/>
   <xsl:for-each select=".//Test">
    <xsl:sort select="count(Set)" data-type="number"/>
     <xsl:if test="position()=last()">
      <xsl:value-of select="count(Set)"/>
     </xsl:if>                    
   </xsl:for-each>
 </xsl:element>
</xsl:template>
Just one thing, why was the variable 'Max' needed?

Neal

A Northern Soul
 
Old September 15th, 2006, 05:28 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You didn't say what you wanted to do with value once you had computed it, so I put it in a variable for you. If you just want to output it, that's not necessary.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Validation For Phone Number and Mobile Number dhruthi.ram99 Javascript How-To 12 October 30th, 2011 07:24 AM
need some help please getting a return value merk ASP.NET 2.0 Basics 4 October 3rd, 2007 07:10 PM
Return dataset lily611 C# 1 July 2nd, 2006 01:35 PM
Return date only...I need your help! billq SQL Server 2000 2 July 1st, 2006 01:56 PM
How to return "0" instead of #Error guizero Access 9 June 2nd, 2006 12:52 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.