Tag Archives: xpath

IDM – using an XPath counter

Using an XPath counter is not really that difficult, but due to the way dirxml script was implemented, one have to split the whole thing in two rules.

First setup a rule which initializes the variable which is used for the counter:


<rule>
<description>setVariables</description>
<conditions>
<and/>
</conditions>
<actions>
<do-set-local-variable name="lv_counter" scope="driver">
<arg-string>
<token-xpath expression="number(0)"/>
</arg-string>
</do-set-local-variable>
</actions>
</rule>

Notice the scope is set to driver, otherwise it will be local to the rule.

Then a rule which is using the counter:

...
<do-for-each>
<arg-node-set>
<token-text xml:space="preserve">//input/*</token-text>
</arg-node-set>
<arg-actions>
<do-set-local-variable name="lv_counter" scope="driver">
<arg-string>
<token-xpath expression="number($lv_counter) + 1"/>
</arg-string>
...

Then for each loop in the do-for-each loop it will count one up.