XPath operator
comment() node test
Selects XML comment nodes. Their delimiters are syntax; the node's string-value is only the content between them.
comment() selects comments on the chosen axis. A comment is a node in the XPath data model even though it is not an element and never appears in an element-only wildcard.
Comments cannot nest and cannot contain --. If the parser preserves them, their string-value is the text between <!-- and -->.
- Syntax
child::comment() — usually comment()- Returns
- node-set
- Kind
- Operator
- Version
- XPath 1.0 (libxml2)
Worked examples
Each result below is what the evaluator returned, asserted on every test run. An empty result means an empty node-set — which is an answer, not an error.
What catches people out
- * selects elements, so */comment() and *[self::comment()] ask very different questions.
- Some application pipelines discard comments before XPath runs; an empty result may describe the tree you were given rather than the source file.
- Comments carry no namespace and have no element name.
The sample documents
Every example on this page runs against this document. The sitemap one is namespaced on purpose — almost every real XML document is, and that changes which expressions match.
<?xml version="1.0" encoding="UTF-8"?>
<order id="A-1001" tier="gold">
<customer country="NO">Ada Lovelace</customer>
<!-- priority customer -->
<?audit retained?>
<lines>
<line sku="WIDGET-1" qty="2"><price>14.50</price></line>
<line sku="WIDGET-2" qty="1"><price>39.00</price></line>
<line sku="GIFT-WRAP" qty="1"><price>0.00</price></line>
</lines>
<note> Deliver after 09:00 </note>
</order>Related
- node() testMatches every node kind on an axis: elements, text, comments, processing instructions, and the root node.
- text() node testSelects text-node children, including indentation whitespace as well as the words a reader sees.
- processing-instruction() node testSelects processing instructions, optionally restricted to one target such as xml-stylesheet.
Get started
Bring order to the XML your team can't afford to ignore.
Create a free account and get a private workspace to search, validate, diff, and monitor your XML feeds, sitemaps, schemas, and vendor integrations.