XML Tutorials - Herong's Tutorial Examples - v5.21, by Dr. Herong Yang
XPath Query with XML Tools on Atom
This section provides a tutorial example on how to use XML Tools package in Atom to run XPath query on any XML documents.
As described in the previous tutorial, "XML Tools" package on Atom offers 3 commands: Pretty-print format, Compress white spaces, and XPath query.
The first 2 commands are not that important. But the last command is important if you are developing XPath expressions to be used in your XSLT templates or other applications. You can open any input XML document and run queries to validate your XPath expressions interactively.
1. Open XML document, dictionary.xml, in Atom:
<?xml version="1.1"?> <!-- dictionary.xml - Copyright (c) HerongYang.com. All Rights Reserved. --> <dictionary> <word acronym="true"> <name>XML</name> <definition reference="Herong's Notes">eXtensible Markup Language.</definition> <update date="2022-12-23"/> </word> <word symbol="true"> <name><</name> <definition>Mathematical symbol representing the "less than" logical operation, like: 1<2.</definition> <definition>Reserved symbol in XML to representing the beginning of tags, like: <![CDATA[<p>Hello world!</p>]]> </definition> </word> </dictionary>
2. Click "Packages > XML Tools > Query Document". The XPath query box shows up.
3. Enter XPath expression "//@date". 1 matched attribute found as I expected. Remember that default context assumes that the root element is the current node.
4. Enter XPath expression "//word[@symbol='true']". 1 matched element found as I expected.
5. Enter XPath expression "//definition". 3 matched elements found as I expected.
6. Enter XPath expression "//definition". 2 matched elements found as I expected.
7. Enter XPath expression "//*[name()='word']". 2 matched elements found as I expected.
Cool. Now I have a nice tool to validate any XPath expression against any XML document.
Table of Contents