XSL Sum() [modified]
-
let say I have this:
<books>
<book>
<title genre="non-fiction">Objects First With Java- A Practical Introduction Using Bluej </title>
<author>David J. Barnes</author>
<author>Michael Kolling</author>
<publisher>Pearson Prentice Hall</publisher>
<year>2006</year>
<price>49</price>
</book>
<book>
<title genre="fiction">Harry Potter and the Half-Blood Prince</title>
<author>J. K. Rowling</author>
<publisher>Thorndike Press</publisher>
<year>2005</year>
<price>29</price>
</book>
</books>and I want to sum the price if the title genre is fiction and book price is less than 30 (I should have more books for this but will add later). currently I read somewhere I came up with this:
<xsl:value-of select="sum(/books/book/title[@genre='fiction']/price[number(text())<30])" />
or this:
<xsl:value-of select="sum(/books/book[title[@genre='fiction']]/price[number(text())<30])" />
but it seems I still cant get it right. Anyone could help where I did wrong? Edited: I found the problem that I should use '& lt;30' instead of <30. I believe I did use lt before asking. Anyway, why the< b r / > in my xsl file does not work in FireFox but it is fine with IE? Many thanks.
modified on Friday, April 17, 2009 8:28 AM
-
let say I have this:
<books>
<book>
<title genre="non-fiction">Objects First With Java- A Practical Introduction Using Bluej </title>
<author>David J. Barnes</author>
<author>Michael Kolling</author>
<publisher>Pearson Prentice Hall</publisher>
<year>2006</year>
<price>49</price>
</book>
<book>
<title genre="fiction">Harry Potter and the Half-Blood Prince</title>
<author>J. K. Rowling</author>
<publisher>Thorndike Press</publisher>
<year>2005</year>
<price>29</price>
</book>
</books>and I want to sum the price if the title genre is fiction and book price is less than 30 (I should have more books for this but will add later). currently I read somewhere I came up with this:
<xsl:value-of select="sum(/books/book/title[@genre='fiction']/price[number(text())<30])" />
or this:
<xsl:value-of select="sum(/books/book[title[@genre='fiction']]/price[number(text())<30])" />
but it seems I still cant get it right. Anyone could help where I did wrong? Edited: I found the problem that I should use '& lt;30' instead of <30. I believe I did use lt before asking. Anyway, why the< b r / > in my xsl file does not work in FireFox but it is fine with IE? Many thanks.
modified on Friday, April 17, 2009 8:28 AM
You were almost there with your second effort:
sum(/books/book[title[@genre = 'fiction']]/price[. < 30])
"We make a living by what we get, we make a life by what we give." --Winston Churchill
-
You were almost there with your second effort:
sum(/books/book[title[@genre = 'fiction']]/price[. < 30])
"We make a living by what we get, we make a life by what we give." --Winston Churchill
Using dot
.& lt;30
seems to be working too and less confusing instead of number(text()). Thanks for your reply.