Share This:

Introduction


As BAO is primarily an XML manipulation tool, XPath is a primary function that will be utilised above the "Basic" functions available and the following site is a great reference to start with in learning command basics:

 

http://www.w3schools.com/xpath/

 

A list of XPath functions is available here that can be used with BAO:

 

XPath, XQuery, and XSLT Function Reference

 

However, these examples list mainly the basics and there are times where more advanced XPath statements are required especially where there are attributes used to contain information and you need to extract a specific value based on an attribute.

The following list some XPath commands to take advantage of some of the more advanced hard to find syntax to extract information from an XML document.

 

XPath Examples

 

Example:

 

<Groups>

  <Group GroupType="ASSIGNEE" GroupID="MEA-CUSTID-GRP-00001">

  <Name>SV.GP.CFS.GSC BTR Service Desk</Name>

  </Group>

  <Group GroupType="RESPONSIBLE" GroupID="MEA-CUSTID-GRP-00002">

  <Name>SV.GP.CFS.GSC BTR Service Desk2</Name>

  </Group>

  <Group GroupType="ACCOUNTABLE" GroupID="MEA-CUSTID-GRP-00003">

  <Name>SV.GP.CFS.GSC BTR Service Desk3</Name>

  </Group>

</Groups>

 

Commands:

 

Extract value based on location in the XML document:

 

//Group[1]/@GroupID

<result>MEA-CUSTID-GRP-00001</result>

 

string(//Group[1])

<result>SV.GP.CFS.GSC BTR Service Desk</result>

 

//Group[2]/@GroupID

<result>MEA-CUSTID-SBSA-GRP-00002</result>

 

string(//Group[2])

<result>SV.GP.CFS.GSC BTR Service Desk2</result>

 

However the above assumes that the XML is a static document and is not changing.

If you have a "dynamic" XML document, then elements and attributes may be generated as required and therefore using a static location will not provide the intended results.  Therefore we need a method to identify and extract the information based on the attribute.

XPath commands can be embedded within each other so as to provide an extended range of functions.

The following shows one method for extracting this information.

 

Example:


<TaskIdentifiers Number="SVR17349937">

  <AltIdentifier Type="PARTNER" Number="PARTNERTSK00005"/>

  <AltIdentifier Type="MAINTASK" Number="MAINTSK00005"/>

  <AltIdentifier Type="RELATEDTASK" Number="CHM17799732"/>

</TaskIdentifiers>

 

Commands:

 

Extract information based on attribute value in the XML document (using embedded XPath):

 

//TaskIdentifiers/@Number

<result>SVR17349937</result>

 

//AltIdentifier[@Type='PARTNER']/@Number

<result>PARTNERTSK00005</result>

 

//AltIdentifier[@Type='MAINTASK']/@Number

<result>MAINTSK00005</result>


//AltIdentifier[@Type='RELATEDTASK']/@Number

<result>CHM17799732</result>



I hope this helps with demystifying XPATH which at times can be quite complex.