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

Re: XSL-parsing problem

Subject: Re: XSL-parsing problem
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Tue, 20 Aug 2002 13:43:40 +0100
xslt gt lt
Hi Niko,

> I have following XML-structure:
>   <includedValues>
>     <range>
>       <start>0</start>
>       <end>2</end>
>     </range>
>     <range>
>       <start>5</start>
>       <end>10</end>
>     </range>
>     <range>
>       <start>15</start>
>       <end>30</end>
>     </range>
>   </includedValues>
>   <answers>
>     <answer>2</answer>
>     <answer>2</answer>
>     <answer>3</answer>
>     <answer>6</answer>
>   </answers>
>
> Included values tells what answers are accepted. In this case
> answers 2 and 6 are accepted, but 3 is ignored. I would like to
> count accepted values to the web page with xsl. Can anyone tell me
> how to do that?
>
> I think I could do that by myself if there was only one range
> (something like
> "count(answers/answer[.&gt;=../../includedValues/range/start][.&lt;=../../in
> cludedValues/range/end])" might do it...) , but document has 0-n
> ranges.

Yes, you could use an XPath if you had a single range, but since there
are several of them you can't because it means you can't keep track of
the value of the answer that you're currently looking at.

Instead, you need to use XSLT. The simplest thing to do is to create a
string that contains a character for each of the correct answers, and
then get the length of that string. Something like:

  <xsl:variable name="ranges" select="includedValues/range" />
  <xsl:variable name="correct">
    <xsl:for-each select="answers/answer">
      <xsl:variable name="answer" select="." />
      <xsl:if test="$ranges[start &lt;= $answer and
                            $answer &lt;= end]">
        <xsl:text>Y</xsl:text>
      </xsl:if>
    </xsl:for-each>
  </xsl:variable>
  #Correct: <xsl:value-of select="string-length($correct)" />

---

With XPath 2.0, I think you'd be able to do it with:

  count( answers/answer
           [some $r in ../../includedValues/range
            satisfies ($r/start &lt;= . and . &lt;= $r/end)] )

as well as rather too many other ways.
            
Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


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.