Skip to content

How do I extract the values from the given path in a XML string

This example demonstrates getting the required values from the XML string by providing a valid path.

In the solution shown below, the XML string and the XML Path are stored into the variables using "store" command. A dataset created using "ddCreateDataset" command and the value is extracted for the defined path into it using "xmlExtractValuesByPaths" command. The value is fetched from the dataset using "ddGetCellValueByIndex" command.

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 <zipcodeDetails> <element> <state>California</state> <zipcode>90001</zipcode> </element> <element> <state>New York</state> <zipcode>10001</zipcode> </element> </zipcodeDetails> XML
Store the XML Path in a variable
2 store //zipcode XMLPath
Declare the dataset name with a data definition specified in the value field containing the column name 'zipcode' where the values from the XML string will be stored
3 ddCreateDataset XMLValuesDatasetXMLValuesDataDefinition(W)
Extract the values from the specified XML path into the dataset
4 xmlExtractValuesByPaths ${XML}||${XMLPath} XMLValuesDataset
Fetch the value that is stored in the first row and first column of the dataset
5 ddGetCellValueByIndex XMLValuesDataset(0,0)zipcode_1

Tips, Tricks, Gotchas & Best Practices:  

  • You can also call XML and XML Path directly in the target field separated by || symbol instead of storing them into a variable.
  • If you have multiple XML paths, you can either provide all the paths in a comma-separated fashion and use the command "xmlExtractValuesByPaths" at once and get the values in multiple columns within a single row or you can use the command 'n' number of times for 'n' number of XML paths. The latter approach will create a row in every occurrence of the command and the values for every path will be extracted in a new row.
  • While declaring the Dataset it is necessary to validate that the Data Definition specified in the value field has a required number of attributes as this would decide the number of columns in the Dataset. If the number of values returned for the given XML path are more than the columns in the Dataset, the command fails with the error "The Number of columns returned are more than the number of attributes in the Data Definition".
  • For the command "ddCreateDataset", the status of the Data Definition can be optionally passed within braces along with the name of the Data Definition in the value field. Allowed values for status are 'W'(WIP-Work In Progress) and 'A'(ACTIVE). If not passed, the status of the Data Definition is assumed to be 'W'.
  • 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.


Feedback and Knowledge Base