Recently I had to integrate a publicly available web service and API with a chatbot I had created in Bot Framework Composer.
The web service was a bit older and used XML. I remember almost 20 years ago integrating XML web services and having to validate and search for data using XPath.
This is a short post to help me remember the syntax when working with XML and XPath in Bot Framework Composer. It may help you too.
The basic logic is:
- Load test XML
- Prompt user for name they want to search
- Store username in a variable
- Run XPath query using username from step #3 to extract data (id)
- Send data to user
The data in step#3 is stored in a dialog scoped variable dialog.username.
We’re using the following XML which contains a few person records:
<?xml version="1.0"?> <people> <person> <id>1</id> <name>Alan</name> </person> <person> <id>2</id> <name>Bob</name> </person> </people>
The following XPath searches the XML for a given person using the name. The value stored in the variable dialog.username is used to fetch the associated id for the person record:
=xPath(dialog.xmldata,"/people/person[name='" + dialog.username + "']/id")
The value  is used to fetch the first node that matches the XPath query.
You can find out more about xPath and Adaptive Expressions here.
The following data is then returned from the XML using the XPath:
You can see the above in action in the following video clip: