XPath is a node query language for XML. The World Wide Web Consortium (W3C) published XML Path Language (XPath) Version 1.0 as recommendation in 1999. XML Path Language (XPath) 2.0 was recommended in 2007 and edited to XML Path Language (XPath) 2.0 (Second Edition) in 2010. The W3C recommendations are posted at:
To use XPath, you need a supporting technology. Many scripting and programming languages support XPath expressions to varying degrees. XPath is embedded in XQuery and XSLT. And when we get to the posts for XQuery and XSLT, we will use XPath expressions. But for our first XPath example, we will query a Flare TOC from the JavaScript console in the Google Chrome web browser.
Flare TOCs are XML documents which create a hierarchical table of contents with titles and links. A Flare TOC establishes the order of topics in a target output, the level or indentation of the topic, and in some cases the heading level of a generated topic. A Flare TOC is an excellent candidate for a query of XML. In this example we look at a generated TOC in HTML5 output. This version of the Flare TOC lives in the Data folder of a target’s output. The TOC is called Toc.xml. Notice the file extension is not FLTOC as in the Flare project.
In a Flare project TOC (FLTOC), a node may link to another TOC. But in the generated output for WebHelp, WebHelp Mobile, and HTML5, these links are replaced with the nested TOC’s hierarchy and links. This content in a FLTOC file:
<?xml version="1.0" encoding="utf-8"?> <CatapultToc Version="1"> <TocEntry Title="Test Topic 1" Link="/Content/TestTopic1.htm" /> <TocEntry Title="Test Topic 2" Link="/Content/TestTopic2.htm" /> <TocEntry Title="Test Topic 3" Link="/Content/TestTopic3.htm" /> </CatapultToc>
May end up as this in HTML5 output:
<?xml version="1.0" encoding="utf-8"?> <CatapultToc Version="1" DescendantCount="3"> <TocEntry Title="Test Topic 1" Link="/Content/TestTopic1.htm" ComputedResetPageLayout="true" ComputedFirstTopic="true" DescendantCount="0" /> <TocEntry Title="Test Topic 2" Link="/Content/TestTopic2.htm" ComputedFirstTopic="false" DescendantCount="0" /> <TocEntry Title="Test Topic 3" Link="/Content/TestTopic3.htm" ComputedFirstTopic="false" DescendantCount="0" /> </CatapultToc>
There are two versions of XPath. But rather than get mixed up in the differences, let’s create an expression to return the titles and links for a Flare TOC. XPath “sees” XML as comprised of nodes. For our XPath expression, we are concerned with the document, element, and attribute nodes.
This XPath expression returns every Link attribute in a TocEntry element in a Flare TOC (FLTOC) file or a Toc.xml file in a target output’s Data folder:
//TocEntry/@Link
Let’s use a supporting technology to evaluate the expression. Here is how to evaluate it with Google Chrome’s JavaScipt console:
Open the Toc.xml file in the browser. If you want to try this with a project TOC file, you’ll need to save it with an XML extension. Copy the file path for the file into the URL field and click the right-arrow to go to it. The XML appears in the browser tab. Click the wrench icon and select Tools > JavaScript console. In the JavaScript console, type the following and press ENTER:
$x("//TocEntry/@Link")
We had to use a technology other than XPath to evaluate the XPath expression and at this point we need some other mechanism to extract the values of each attribute. XPath does not support extracting attribute values. The natural choice is to use whatever technology evaluated the expression. So we will stop here until we discuss technologies which support evaluating XPath expressions.