This is my xml Document.
<w:document xmlns:w="w">
<w:body>
<w:p>
<w:r>
<w:t>
Para1
</w:t>
</w:r>
</w:p>
<w:p>
<w:r>
<w:t>
Para2
</w:t>
</w:r>
</w:p>
<w:p>
<w:r>
<w:t>
Para3
</w:t>
</w:r>
</w:p>
<w:p>
<w:r>
<w:t>
Para4
</w:t>
</w:r>
</w:p>
<w:p>
<w:r>
<w:t>
Para5
</w:t>
</w:r>
</w:p>
<w:tbl>
<w:tr>
<w:tc>
<w:p>
<w:r>
<w:t>
Para6
</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:p>
<w:r>
<w:t>
Para7
</w:t>
</w:r>
</w:p>
</w:tc>
</w:tr>
</w:tbl>
<w:p>
<w:r>
<w:t>
Para8
</w:t>
</w:r>
</w:p>
<w:tbl>
<w:tr>
<w:tc>
<w:p>
<w:r>
<w:t>
Para9
</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:p>
<w:r>
<w:t>
Para10
</w:t>
</w:r>
</w:p>
</w:tc>
</w:tr>
</w:tbl>
<w:p>
<w:r>
<w:t>
Para11
</w:t>
</w:r>
</w:p>
</w:body>
</w:document>
Now, I want to increment my global variable whenever <w:tbl><w:tr> encounters.for my above xml file,it has two <w:tr> nodes inside <w:tbl>.
So, for example :
1. if my current node is Para8 then it's count will be 1.
2. if my current node is para11 then it's count will be 2.
How i do it?
XSLT is a functional language and in any functional language the value of a variable, one set, cannot be updated. One needs a “paradigm shift” — to start thinking in a functional way — in order to understand that the “capability” to update variables isn’t necessary at all. For any imperative algorithm (that uses variable update) there is a corresponding functional algorithm (that doesn’t require update of any variable).
There are a number of advantages using a functional programming style over an imperative one — the main being that a functional program is much better to read, understand, maintain and even prove correct. Due to variables being immutable, the optimizer of a compiler can perform much more aggressive optimizations and this results in more efficient, highly optimized compiled programs.
In this particular case, it isn’t necessary to update a variable in order to get the required count:
When this transformation is applied on the provided XML document:
the wanted, correct result is produced:
In case this value should be contained in a variable — to be used later somewhere in its scope, one will simply define the variable as:
And you can use this variable as in the transformation below:
producing the same correct result: