Skip to content

How do I get the depth and length of a XML string

This example demonstrates how to get the depth and length of the XML string for the defined path in the Target field. 

In the solution shown below, the XML document is stored into the variable using "store" command. The depth of the XML is fetched using "xmlGetDepth" command and the length of the XML is fetched using "xmlGetLength" command by providing the path seperated by "||" symbol in the Target field.

Note: The notation to reuse the value of the variable in Worksoft SaaS is ${<Variable Name>}.

Solution:

# Command Target Value
Store the XML in a variable
1 store <?xml version="1.0" encoding="UTF-8"?> <insuranceData> <insurance category="valid"> <age>45</age> <income>55000</income> <zipcode>90001</zipcode> <result>You are eligible for a subsidy.</result> </insurance> <insurance category="valid"> <age>64</age> <income>25000</income> <zipcode>14301</zipcode> <result>You are eligible for a subsidy.</result> </insurance> <insurance category="invalid"> <age>15</age> <income>25000</income> <zipcode>08857</zipcode> <result>Combinations of inputs provided out of scope for the calculator.</result> </insurance> </insuranceData> XML
Get the Depth of the XML and store it in a variable
2 xmlGetDepth ${XML}
depth
Store the XML Path in a variable
3 store
//insuranceData//age
XMLPath
Get the Length of XML for the defined path and store it in a variable
4 xmlGetLength ${XML}||${XMLPath}
length

Tips, Tricks, Gotchas & Best Practices:  

  • You can also call XML directly in the Target field instead of storing them into a variable.
  • The XML should be valid and encoded in "UTF-8" format.
  • As a best practice, it is always advisable to define the variable names without any spaces and if you want to differentiate the words, use underscores.
  • "xmlGetLength" command returns the number of occurances of the XML path in the XML document.


Feedback and Knowledge Base