XPath Editor Online: Check the Namespace and the Result Type

An XPath returning zero nodes may be using the wrong namespace rather than finding an empty document. Preserve the XML and bind the query prefix explicitly before interpreting the result.

Parse a separate XML document, evaluate the expression, and label numbers, strings, booleans, and node selections distinctly. Empty strings and numeric zero remain completed results instead of disappearing as falsey values.

XPath Editor Online: Check the Namespace and the Result Type
XPath Editor Online: Check the Namespace and the Result Type
Open the full example ↗

Keep the default namespace in the source

Use the exact original XML:

<catalog xmlns="urn:demo:catalog"><item code="A1">0</item><item code="A2">7</item></catalog>

The catalog and item elements belong to urn:demo:catalog. Declare a resolver binding c → urn:demo:catalog for the expressions. The query prefix c need not appear in the XML source; it is the expression's way to identify that namespace.

MDN explains why unprefixed XPath element tests do not automatically use an XML default namespace and describes explicit prefix resolution. Keep the URI visible beside the expression rather than silently rewriting the source. XPath namespace resolution

Give each expected result its own type

Expression Expected kind Expected result for this fixture
/catalog/item Node selection No nodes
/c:catalog/c:item Node selection Two item element nodes: A1 with text 0; A2 with text 7
/c:catalog/c:item[2]/@code Node selection One attribute node, code, value A2
count(/c:catalog/c:item) Number 2
string(/c:catalog/c:item[1]) String "0"

The first result is a namespace mismatch, not proof that the document has no item elements. The third selects an attribute, not the second item's element or its text 7. The last returns a string containing one character, not an absent value.

The historical XPath 1.0 specification defines unprefixed name tests, counting nodes, and converting node selections to string values. Use it for this version's semantics, without presenting it as the latest XPath specification or claiming XPath 2/3 support. XPath 1.0 specification

For further expected-state checks, count(/catalog/item) returns number 0, while string(/catalog/item) returns the empty string "". Those differ from a no-nodes selection and from the first item's string "0". Each needs an explicit label.

Do not use a generic “if there is no value” display rule that collapses these states. Include not evaluated and error too. A zero count is a completed numeric result; an invalid expression is not a zero count.

Evaluate a separate document and read the matching result property

The workbench has an XML field, an expression field, a labeled namespace binding, Evaluate, and Clear. Parse a separate XML document for a particular evaluation revision, then keep it unchanged while inspecting the result. Do not evaluate against the article's live HTML document or retry malformed XML as HTML.

This example first checks syntax in a detached XML document using Range.createContextualFragment, then requires one root and valid surrounding whitespace or comments. A legitimate parsererror element remains data. XPath still evaluates the original separate document after this check; malformed XML never becomes a successful node selection.

MDN's evaluation API distinguishes numeric, string, boolean, and node results. Read the result property appropriate to its type; a scalar is not a node list. For node selections, specify a document-order display and label element versus attribute kind. Document.evaluate result types

Record the XML revision, exact expression, bindings, result type, literal scalar or ordered node descriptions, and any diagnostic. Display those descriptions as text. Do not insert selected nodes into the page as live content; selecting an XML element is not permission to render its markup or follow a URL contained within it.

Clear stale answers on every input change

An edit to XML, expression, or binding invalidates the previous result and returns the view to not evaluated. XML parse errors, expression errors, and unresolved-prefix errors clear prior output and show a current diagnostic. Keep the source text available for correction.

For example, changing c's binding to another URI can produce a valid empty selection; removing the binding while still using c should produce a resolution error. These have different explanations. Do not silently bind every unknown prefix to the fixture URI just to get a match.

Clear resets the input fields, binding, result, and diagnostics to an empty not-evaluated workspace. Keep clearing the workspace separate from restoring the original XML and c binding; neither action counts as an evaluation.

Set original workload limits of 10,000 XML characters, 1,000 expression characters, and 100 displayed result nodes. Count input lengths in UTF-16 code units. Over-limit input remains intact with a limit issue; over-limit results receive an explicit limit status rather than a truncated list presented as complete.

Try the worked example

Evaluate the default prefixed selection to inspect two item elements. Change the expression to count(/c:catalog/c:item) for Number:2, then string(/c:catalog/c:item[1]) for String:"0". Remove the binding to expose a prefix error; unprefixed element tests instead give a valid empty selection. Zero nodes, number 0, and string "0" are different results.

This uses browser XML/XPath 1.0 APIs, not schema validation, XSLT, remote retrieval, or XPath 2/3.

Recreate this example

Build one complete self-contained HTML page for this task: evaluate bounded XML/XPath with explicit namespace binding and typed output.

Use these exact original practice data and worked expectations: <catalog xmlns="urn:demo:catalog"><item code="A1">0</item><item code="A2">7</item></catalog>. Bind c to urn:demo:catalog. /catalog/item selects 0 nodes. /c:catalog/c:item selects 2 elements. /c:catalog/c:item[2]/@code selects attribute code = A2. count(/c:catalog/c:item) returns Number 2. string(/c:catalog/c:item[1]) returns String "0".

Implement the following useful local interactions: XML, expression, binding fields; Evaluate and Clear. Validate supported input with Range.createContextualFragment in a detached XML document; require one root and valid surrounding whitespace/comments. Accept legitimate parsererror elements and reject malformed XML or multiple roots. Then use DOMParser and Document.evaluate on the original separate document. Show typed scalar or document-order node descriptions as text. Input edits clear output. Reject XML/expression limits of 10,000 and 1,000 UTF-16 code units respectively; DTDs, entity declarations, processing instructions, and XML declarations. Limit output to 100 nodes. Malformed XML or prefix/expression errors clear stale results. Preserve the supplied source text and distinguish an uncomputed state, a successful local result, and an invalid or incomplete input. Update output only from the current input snapshot. An input edit must retire stale feedback until the reader runs the relevant action again. Do not silently invent missing values or replace a failing case with a success.

Design the page as a compact source-and-result developer workbench with readable code, labeled inputs, and a separate explanation area. Put a compact task heading, actual sample data, the main result, and usable controls early enough to be useful on a narrow screen. Use readable text of at least 16px, generous paragraph spacing, strong contrast, native labels, accessible buttons, and visible keyboard focus. Stack long records at 360px and wrap code without page-level horizontal overflow.

Use type=button and explicit querySelector-based click handlers rather than relying on form submission. If Enter is useful, handle it only on the relevant input and ignore isComposing. Keep effects within this open page; no remote dependencies, network calls, persistence, fake downloads, or automated sending. This uses browser XML/XPath 1.0 APIs, not schema validation, XSLT, remote retrieval, or XPath2/3.  Place worked explanations beside their data and identify teacher or synthetic examples clearly. Output only the complete HTML document, with inline styles and script and LF line endings.

Review the namespace-aware XPath 1.0 workbench in NOS

Retain the XML revision, expression, binding, and typed result when transferring a query into your own tool. Use the explanation in NOS to review why the unprefixed query finds no nodes while the c-prefixed query finds two, then revise eligible static namespace instructions in visual editing mode.

Keep reading