Hi, I'm new to xpath. I have some question I dont know how to do it.. I've googled about it and i dont see any solution.

Question:

I wanted to do a math calculation.(this i know how to do) but then i wanted it to display it out using my xpath query..
can someone be kind enough to show me some simple calculation which it will come out with the number??

exp: //Studentsum(Subjects/Marks)>100 - this is just to show out the which student have the marks more than 100. But what i want is i wanted to display out the total Marks ( number ) Any idea??

2nd Question:

I'm just wondering.. can xPath put the numbers or string... into a variable?? ^^ just wondering..

Thanks for anyone who helping me.. cause I'm realy new to xpath..

Or do you all have any link which can teach me how to display out the number / putting the number into a variable? ^^

Best Regards,
Hongz

Member Avatar for gravyboat

(1) XPath has the sum() function; I'm not sure exactly what you're looking for, but if you want to display the total <marks>, you would write

<xsl:value-of select="sum(//marks)"/>

(2) XPath can be used to *assign* a variable, but it's up to the language using XPath (XSLT, VB, Java, etc.) to create the variable.

<xsl:variable name="totalMarks" select="sum(//marks)"/>

The variable $totalMarks now contains a number.

<xsl:value-of select="$totalMarks"/>

I'm really appreciate for your reply.. thanks a lot..

But what i'm handling now isn't related to xsl
I'm using a program which will get xpath queries..

xml data :

<Students>
<Student>
<FirstName>Yien Yien</FirstName>
<LastName>Lo</LastName>
<Address>London</Address>
<Faculty>Business</Faculty>
<Year>2007</Year>
<Subjects>
<Subject>
<Code>MEEA3000</Code>
<Marks>81</Marks>
</Subject>
<Subject>
<Code>JKEA1200</Code>
<Marks>18</Marks>
</Subject>
<Subject>
<Code>MEEA1000</Code>
<Marks>67</Marks>
</Subject>
<Subject>
<Code>SMES1104</Code>
<Marks>47</Marks>
</Subject>
</Subjects>
</Student>
.
.
.
</Students>


queries : /Students/Student[count(Subjects/Subject)=0]
result : this will search student that register but did not take any subject


I didnt use any xsl on this.. i wanted to make a queries that can sum up the marks for that student.. am i possible get the sum for the marks by using only xpath queries??

Is it possible? ^^

Best Regards,
Hong

Member Avatar for gravyboat

The XPath will be the same, regardless of whether you're using XSLT or a programming language; just remove the expression from the "select" statements.

E.g.,

sum(/Students/Student//Marks)

Will sum the marks for all Students, or if you're looping through all Students

sum(//Marks)

(assuming that the XPath is operating from within a loop of Students).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.