Excel is typically where data ends up being collected. User inputs, data exports, and even web services can provide such data. There are various tools and even some built-in Excel features that can help with this sort of operations, but nothing beats a good old formula.
Excel 2013 introduced 2 new ‘special functions’. Unlike others, these two formulas help access and organize data that is outside your workbook.
The WEBSERVICE function can directly access web sites and web services on the internet and grab data in XML format, while the FILTERXML function can parse that XML string and filter it to return the data in whatever format you’d like.
For those who are not familiar with the concepts of web services or XML, let’s begin with a background. Web services are a great way to send and receive data via the internet. Object based data moves between web-based applications and software, in XML or JSON forms. Stock prices, weather forecasts, and geographical data are common examples of web services being utilized.
XML is one of the most used standard of data transfer. It stands for eXtensible Markup Language. This is a language that contains a set of rules for encoding data to make the data both readable for the user and machines.
Although Excel has other ways to access the data through internet, the WEBSERVICE and FILTERXML functions can give Excel an active role in this data transferring protocol. Instead of learning VBA coding or PowerQuery’s script-based language, a formula-based approach can be easier for most. You can download our sample workbook below.
The WEBSERVICE function returns the XML based data from a website or web service on the Internet or Intranet. The only argument it requires is the URL of the website or web service. If the connection is successful, the formula cell will show the XML output.
In the example below, we used a web service that returns current stock prices when you enter the stock symbol (i.e. “MSFT” for Microsoft Corp.).
Here, we copy and paste the URL into any cell (i.e. B2) and use the WEBSERVICE function with the reference of the URL cell.
The result is typically a long string that contains tags like Name, Symbol or LastPrice. If you’d like to see the outcome data in a more user-friendly way, click the URL. You will get the same output in your browser.
Although return value contains useful data, you’re going to want to organize it a bit to make it easier to read, and that’s where our second function, FILTERXML, comes into play.
The FILTERXML function can find and get the value we need without our eyes got hurt. The function requires two arguments: The first is the XML text, and the second is the query language called XPath. This, of course, doesn’t mean you need to learn a programming language.
The XPath stands for XML Path Language, and it uses a “path-like” syntax to identify the where the data is to be placed. “Path-like” here is a string that starts with double slashes and a single slash for each folder (tag operator in XML). In our stock quote example, we need a path like below to get the data.
This path targets the Name tag under Data tag which are under the QuoteApiModel tag.
An alternative way to use the FILTERXML function is to use it as an array formula to return all values in same tags. This type of usage comes in handy if the return XML contains similar data, such as dishes in a breakfast menu.
Here is another web service that returns a list of stock values by a specified keyword. We used “software” keyword in our example. Since the stock market doesn’t consist of a single company, we again get a long XML string with same tags used more than once, signifying the same kind of data with different values.
To get the symbol names we need to use a path like below.
If you try creating this path the same way as before, you will only get the first symbol of stocks. The reason behind this is that the function can get all symbols as an array, but can only show the first one, if it is not set as an array formula. To convert a regular formula into an array equivalent can be done like this:
- Begin by selecting a range that contains the same number of cells as the return values. For example, B9:B16
- Type in your formula (i.e. =FILTERXML(B7,B8) ). Cell B8 contains the same XPath string: //LookupResultList/LookupResult/Symbol
- Press the Ctrl + Shift + Enter key combination instead of Enter.
Now you should get all symbols together.
Please note that the WEBSERVICE function can only work with services that do not require authentication. An alternative way to do this is by using Microsoft’s Power Query add-in. Power Query comes with more features that are suited for working with web services as it can handle authentication, and has the ability to work with either XML or JSON.
Also please note that this function relies on some Windows features and will not return results on a Mac.