Data analysis guides

Test an XPath predicate against two library shelves

·

A library feed has two shelf elements. A map can appear on either shelf, so inspect which predicates narrow the selection before saving the XPath.

XPath Finder and Tester

Read the match, count and attribute in the same run

Select Load XML example and Run XPath. Line 1 finds Café map on the east shelf. Line 2 returns the number 2 because both shelves contain a map. Line 3 selects the area attribute and shows west.

Use path in the first match. The expression becomes /library[1]/shelf[1]/item[1]. Run it and check that Café map is still the only match.

W3C XPath 1.0 — Location paths

Use a prefix when the document declares a default namespace

Replace the input with <library xmlns="urn:library"><item>Café map</item></library>. Compare //item and //default:item on separate lines. The first has no matches. The second selects the item because default: resolves to the root XML namespace.

For a custom prefix, enter shelf=urn:library in Namespace mappings and test //shelf:item.

W3C XPath 1.0 — Location paths

Save the report and inspect line numbers

With the XML example loaded, add a blank line and an incomplete expression such as //item[. Run XPath and download the report. Its JSON keeps the original query line numbers. Look for the one-match entry, the numeric value 2 and the final syntax error.

Common questions

Do I need a namespace prefix for a normal HTML article element?

In HTML mode use //article. The HTML parser also makes implicit html, head and body elements available in the tree.

Why does the output list fewer nodes than the count?

A query reports the full number of matches and shows the first 25 nodes with their paths.

Sources

Put it into practice.

Related guides