Interacting with XML-based web services using SPARQLMotion
One of the new features of TopBraid 3.0 is improved handling of web services that return XML results. We have added two new module types to SPARQLMotion that can be used to walk through XML documents via XPath. Here is an illustrated example of how to use these capabilities. You can also download and play with the example script.
The first step is a web service call using sml:ImportXMLFromWorkspace:
In this example we are using a fixed URL from the geonames.org web service:
We can run this trivial script with the debug button to examine the XML result in TopBraid's console:
Our query takes a postal code as input and delivers an XML structure of matching Australian suburbs. We want to copy some of the result values into our simple Suburbs ontology, and capture name, latitude and longitude of each suburb.
The result is that we have converted the XML data from the web service call into instances of the my:Suburb class in our ontology:
In order to do that, we want to walk through all code tags of the XML result. We add an sml:IterateOverXPath module that takes the ?xml from the previous step as input:
At this stage, Composer offers a convenience feature: right click on the IterateOverXPath module and select "Set XPath...": this will execute the script up to that module and open up an XPath creation dialog. This dialog can be used to select the path to the elements that we want to iterate over:After we have defined and assigned the XPath, we can now specify what we want to do with each code element from the XML document. In order to do so, we create the body of the iteration module as shown in the overview below. This body will be repeated for each code element. The ?xml variable is then bound to the current code element as root.
Each of the three sml:BindByXPath modules has a local XPath starting at the respective code element. Again, we can use the "Set XPath..." feature to let the SPARQLMotion engine execute up to the module and open an XPath editor dialog:We simply need to pick the example instance data (here, the specific latitude value) and the dialog will generalize it into an XPath expression in the text field. This will bind the specified text node to the variables ?name, ?lat and ?long. In the final step, we define a SPARQL Construct query using sml:ApplyConstruct to create RDF triples for those values:
The complete script can now be executed, and SPARQLMotion we can infer the resulting triples so that we can browse them in TopBraid: