Using XPath
What is XPath
XPath is a language for finding information in an XML document. XPath models an XML document as a tree of nodes. XPath expressions look very much like the expressions you see when you work with a traditional computer file system, for example:
|
|  |
Select all "chapters" within "book". |  |
|
/book/chapter[@title="Anomaly"] |  |
Select all "chapters" within "book" with attribute title "Anomaly". |  |
|
|  |
Select all "chapters" anywhere in the document. |  |
|
//chapter[2] |  |
Select the second "chapter" anywhere in the document. |  |
|
//comment() |  |
Select all comment in the document. |  |
XPath and namespaces
To select nodes in a namespace, a prefix must be used to indicate the namespace. For example, the following XML contains three namespaces:
|
prefix |  |
namespace |  |
|
cats |  |
{http://www.xmlblueprint.com/cats} |  |
|
dogs |  |
{http://www.xmlblueprint.com/dogs} |  |
|
no prefix |  |
{http://www.xmlblueprint.com/guinea-pigs} |  |
To select nodes in namespace {http://www.xmlblueprint.com/cats}, prefix "cats" must be used. For instance, the XPath expression "//cats:cat" selects all cats in namespace {http://www.xmlblueprint.com/cats}:
<cats:cat breed="British Shorthair"/>
<cats:cat breed="Burmese"/>
<cats:cat breed="Egyptian Mau"/>
<cats:cat breed="Siamese"/>
To select nodes in namespace {http://www.xmlblueprint.com/dogs}, prefix "dogs" must be used. For instance, the XPath expression "//dogs:dog" selects all dogs in namespace {http://www.xmlblueprint.com/dogs}:
<dogs:dog breed="Cocker Spaniel"/>
<dogs:dog breed="Golden Retriever"/>
XPath and the default namespace
To select nodes in namespace {http://www.xmlblueprint.com/guinea-pigs}, a prefix must be used. But, because namespace {http://www.xmlblueprint.com/guinea-pigs} is the default namespace, there is no prefix bound to this namespace. In XMLBlueprint you may choose your own prefix for the default namespace, the default prefix is "default".
Using XPath in XMLBlueprint
To use XPath in XMLBlueprint, open the Explorer (View > Show Explorer) and select tab XPath.
Open an XML document you want to query.
Type an XPath expression (e.g. "//xsl:template") in the edit box and press enter or click Select.
The results of the XPath query are returned below the edit box. Click on a node to select the node in the active editor.