Software Architect / Microsoft MVP (AI) and Technical Author

Bot Framework, Chatbots, XML, XPATH

How to Find Data in XML using XPath and Adaptive Expressions in Bot Framework Composer

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:

  1. Load test XML
  2. Prompt user for name they want to search
  3. Store username in a variable
  4. Run XPath query using username from step #3 to extract data (id)
  5. 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:

JOIN MY EXCLUSIVE EMAIL LIST
Get the latest content and code from the blog posts!
I respect your privacy. No spam. Ever.

Leave a Reply