XPath

Introduction

This section is designed to introduce you to XPath and its power when combined with 3B2. Information covered includes how XPath itself was created, its fundamentals and how it is incorporated with 3B2 in order to provide the user with an easy, concise and practical tool for developing templates.

What is XPath?

The XPath language was created in order to fulfill the operational needs of XSLT, the XSL transformation language, and XPointer. Both specifications needed a way to point to a specific contextual item or a set of items in a well-formed XML document.

Consequently 3B2 uses XPath as a foundation and uses uniquely developed extensions to format XML documents.

Principles of XPath

The key to addressing certain parts of an XML document is the location path. A location path describes how something may be found. By way of analogy, you can think of a location path as describing how to find a certain paragraph in a document, for example, 'the second paragraph in the third chapter.'

XPath treats an XML document as having a tree structure and uses some genealogical terms to describe the levels of the tree. In the tree there are seven types of node which match each type of construct that can appear in an XML document; element, attribute, text, processing instruction, comment, namespace and root. It is these nodes that are the addressable parts of the document. The root node is the root of the document (every XML document has exactly one root node which can be considered as the container of the whole document).

XPath is not only concerned with locations. It also allows certain functions and expressions to be performed on the nodes selected by the location path, such as context tests, selecting parts of the content of the node and counting the number of nodes selected by the location path. Below is an example of a tree structure that includes five node types. The Doc, Chap and Para nodes are elements.

Why XPath in 3B2?

XPath syntax can be used to quickly address parts of an XML document and therefore can be used to perform long-range context tests which would have previously required multiple formats. XPath can be used in conjunction with 3B2 to perform tasks that XPath alone is not designed for. Extra functions were added to 3B2 in order to allow XPath to execute commands specific to 3B2, such as showstring like functions, gathering content and mark-up and finding the start and end positions of nodes. XPath can also be used inline to display the results of the expression and pass to variables and tags using the normal syntax.

Overall Benefits

The addition of the XPath syntax in 3B2 promises to greatly enhance 3B2 template creation and design, making it even easier and quicker. XPath usage will minimise the need for counters, yanks and other showstring expressions as each test or manipulation can be performed when and where it is required.

Further information

You can find out more on XML and XPath by visiting:

The W3C website www.w3c.org/tr/xpath

The XML website. News, education, and information about XML in industrial and commercial settings:

www.xml.com


See also