Here is the sample xml document.
count the number of words
For this example I want
Use this XPath one-liner:
string-length(normalize-space(node))
-
string-length(translate(normalize-space(node),' ','')) +1
Here is a short verification using XSLT:
When this transformation is applied on the provided XML document:
count the number of words
the wanted, correct result is produced:
5
Explanation: Use of the standard XPath functions normalize-space(), translate() and string-length() .
Update1:
The OP asked:
"Your (Dimitre Novatchev) code is working fine for the above xml. Is your code will work for the following xml?"
pass pass fail pass fail pass pass fail
Answer: The same approach can be used:
When this transformation is used on the newly-provided XML document (above), the wanted correct answer is produced:
8
Update2: The OP then asked in a comment:
"Can I have a comparision with the words in the node with some default word. Conside node contains value
"pass pass fail". I want to calculate number of pass and number of fail. LIkepass=2 fail=1. is it possible? Help me man"
Answer:
The same approach works with this modification of the problem, too (in the general case, though. you need a good tokenization -- ask me about this in a new question, please):
pass:
fail:
When this transformation is applied on the last XML document (above), the wanted, correct is produced:
pass: 2 fail: 0
pass: 1 fail: 2
pass: 2 fail: 1