Implementing the Tableau 9.0 REST API — Part 3 — XML Parsing

XML

The “eXtensible Markup Language” is the basis for all messages in the Tableau REST API. Both your requests to and your responses from the Tableau Server will be made in UTF-8 encoded XML. In general, the Tableau REST API schema keeps all information in XML attributes, so there is no need to constantly determine if the info is in an attribute or in the text between the tags. However, there are two parts of the XML standard that are worth understanding and making sure you have facilities for.

XPath

XPath is the query language for the Document Object Model used in XML (and HTML). You bought Tableau so you wouldn’t have to write queries anymore, right! XML documents are not relational tables, but tree-models, and XPath was specifically designed to query this type of data structure effectively, making it very useful for finding exactly what you want in an XML document, regardless of where it is located or how many results there are. On the other hand, XPath bears zero resemblance to SQL, so you’ll have to learn to read and think in document trees with a whole new syntax.

XPath may not look familiar but it lets you answer some very interesting questions.

For example, when you query Workbook Permissions, there are a number of tags that have nested tags inside.


<granteeCapabilities>
<group id="group-id" />
<capabilities>
<capability name="capability" mode="capability-mode" />
</capabilities>
</granteeCapabilities>

The following XPath query will bring back matches all of the capabilities with a given name attribute that fall under the tag that has tag with the given group_luid

//t:granteeCapabilities[t:group[@id=’$group_luid’]]/t:capabilities/t:capability[@name=’$capability_name’]

We will see more examples of XPath as we work through more of the library. You’ll also notice the “t:” prefix used in the XPath query. This is a short alias for the Tableau REST API XML Namespace, and it is essential.

Namespaces

XML has a concept called Namespaces, and the Tableau REST API definitely has a declared namespace. In the PHP library, we register ‘t’ as an alias for the Tableau namespace so that later we can use the short letter before returning any XML response. You have to prefix every element you search for in an XPath query with the namespace or its alias, or it will not parse correctly and will return nothing.


public function getResponse(){
if($this->response_type == 'xml' && $this->simple_xml_object !== null){
// Register a namespace alias for xpath queries
$this->simple_xml_object->registerXPathNamespace('t', 'http://tableausoftware.com/api');
return $this->simple_xml_object;
}
else {
return $this->raw_response;
}
}

http://www.w3schools.com/xml/xml_namespaces.asp

XML Parsing Library

You’ll need to decide on a code library for parsing and working with XML. None of the XML structures are complex, but you’ll want something with facilities for handling namespaces, XPath, and easy iteration.

In the two example libraries,I’ve chosen SimpleXML( http://php.net/manual/en/book.simplexml.php ) in PHP and lxml ( http://lxml.de/ ) in Python. Both provide easy iteration using their language’s native facilities and also full XPath querying. ElementTree in Python does not have a complete set of XPath abilities, or as easy of a syntax for dealing with namespaces. You will see them both used as we dive into the libraries..

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s