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.
Chatbot Logic
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.
Example XML
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>
XPath Query
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 + "'][1]/id")
The value [1] is used to fetch the first node that matches the XPath query.
You can find out more about xPath and Adaptive Expressions here.
Returned Data
The following data is then returned from the XML using the XPath:
<id>2</id>
Demo
You can see the above in action in the following video clip:
Leave a Reply