Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to count ?

 
jim yin
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following xml file, in order to count # of employees with salary of 100000, for example, how to do it?

<employees>
<employee hireDate="04/23/1999" officer="yes">
<last>Hill</last>
<first>Phil</first>
<salary>100000</salary>
</employee>
<employee hireDate="09/01/1998" officer="no">
<last>Herbert</last>
<first>Johnny</first>
<!-- Salary may need updating -->
<salary>95000</salary>
</employee>
<employee hireDate="08/20/2000">
<last>Hill</last>
<first>Graham</first>
<salary>89000</salary>
</employee>
<employee hireDate="10/16/2000">
<last>Sterling</last>
<first>Moss</first>
<salary>97000</salary>
</employee>
</employees>
 
Jayadev Pulaparty
Ranch Hand
Posts: 662
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This will do it -
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:value-of select="count(//salary[.=100000])"/>
</xsl:template>
</xsl:stylesheet>
 
jim yin
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jayadev Pulaparty:
This will do it -
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:value-of select="count(//salary[.=100000])"/>
</xsl:template>
</xsl:stylesheet>

Thanks, Jayadev. I am still not very clear about
the following part:

count(//salary[.=100000])

salary is a subelement, "." means contxet node,that is salary, right?
"100000" is the text node subelemnt of salary, not a attribute. How can we add this as a predicate?
 
Jayadev Pulaparty
Ranch Hand
Posts: 662
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"." here means the string value of the current context node (salary). The string value of a node is same as the content of its text node. I guess xpath is smart enough to figure out a number Vs a string when it comes to comparisons.
I have a very useful OReilly XPath chapter downloaded free from the internet at some website which i don't remember. I can send it to u in an email if required.
 
Jayadev Pulaparty
Ranch Hand
Posts: 662
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim,
I could search the link for the free OReilly xpath chapter. The link is a little slow and hence a little more patience is required
http://www.oreilly.com/catalog/xmlnut2/chapter/ch09.pdf
 
Dan Drillich
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or -
http://www.oreilly.com/catalog/xmlnut/chapter/ch09.html
 
jim yin
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much, guys.
[ September 11, 2002: Message edited by: jim yin ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic