Accessing Comments in XML using XPath

xpath comment contains(text)
xpath examples
xpath tester
xml xpath
xpath tutorial
xpath cheat sheet
comment in xpath
xpath web scraping

How to access the comments inside the XML document using XPath?

For example:

<length> 12 </length>
<!--Some comment here-->

I want to access the "Some comment here".


EDIT: I am using MSXML DOM ActiveX and the command comment() seems to be failing... Any idea why?

With the path


you can select all comments in the /foo/bar element. May depend on your language of choice, of course. But generally this is how you do it.

XPath - Comment Node, xml and its stylesheet document students.xsl which uses the XPath expressions. Following is the sample XML used. students.xml. <?xml version� XPath is a syntax for defining parts of an XML document. XPath has been around since 1999 and has been used as a standard way to query XML documents. XPath defines an XML document as a tree. Each node inside that XML document has a relationship to other nodes as a sort of family tree. If one node contains many other nodes, the single node would

Use comment() function for example:-


selects the first comment that follows the length element.


Manoj asks in a comment to this answer why this isn't working in MSXML. The reason will be you are using MSXML3. By default MSXML3 does not use XPath as its selection language, it defaults to an earlier much weaker language (XSL pattern). You need to set XPath as the selection language via the DOMDocument's setProperty method. E.g (in JScript):-

var dom = new ActiveXObject("MSXML2.DOMDocument.3.0");
dom.setProperty("SelectionLanguage", "XPath");

Now the full XPath language will work in your queries (note one breaking change is indexer predicates are 1 based in XPath whereas they were 0 based in XSL Pattern).

[2.0] Comments in XPath Expressions, Take O'Reilly online learning with you and learn anywhere, anytime on your phone or tablet. Download the app today and: Get unlimited access to books, videos,� Now, we are going to access the XML data from XML document by using XPath. XPath XPath is used to navigate through elements and attributes in an XML document. In other words, XPath uses the path expressions to select nodes or node-sets in an XML Document. Here is the code: By using XmlNode keyword, we can access the node value.

Based on the OP's comments to posted answers (and my curiosity as to why this simple thing would not work), here is my suggestion:

Using the XPath expression suggested by @Anthony, I was able to successfully load the comment node with the following JS function:

function SelectComment(s)
  var xDoc = new ActiveXObject("MSXML2.DOMDocument.6.0");
  if (xDoc)
    var selNode = xDoc.selectSingleNode("/table/length/following::comment()[1]");
    if (selNode != null)
      return selNode.text;
      return "";

Sample invocation:

SelectComment("<table><length> 12</length><!--Some comment here--></table>");


"Some comment here"


a. Your MSXML version may vary. Please use appropriately.

b. This kind of code is definitely not recommended because it works only on IE. However, since this is your explicitly stated requirement, I have used the ActiveXObject.

c. You have not mentioned in your comments what fails in the suggested XPath expressions. My guess is that you are not querying the text property of the retrieved node. Keep in mind that the SelectSingleNode always returns an IXmlNode and you need to query its data or text properties.

An Introduction to XPath: How to Get Started, It allows you to select nodes or compute values from an XML or With XPath, you can extract data based on text elements' contents, and not only on the page structure. Comment node: represents comments in the document ( <!-- … We can access any node attribute using the @attributename syntax. Using XPath Expression to Access or Read XML document in ASP.Net XML is one of the widely used data exchange format for variety of reasons in software industry. Hence, working on XML data like selecting a XML node, navigating the XML node tree for finding a node or doing some manipulation on XML nodes has become one of the major and repetitive

Maybe this coud help, This sample removes Comments

XmlNodeList list = xmlDoc.SelectNodes("//comment()");
foreach(XmlNode node in list)

Leaned from here link text

XML and XPath, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java� XPath may look a bit intimidating to people unfamiliar with the syntax. Let's see if we can go through an example explaining the format of a simple xpath. To start off, we'll be using the xpath command for set, which lets us change an attribute, or value. Another post will show more of xpath commands, but we'll begin with the simplest.

<!-- krishna k -->
<name>FX Update USD</name>
<!-- Since this plan updates existing adj's no ajd's will be created using this id -->
Am facing the similar Issue my application is reading comments which causes stack crash. How can I avoid reading comments by DOM.

xsl:comment, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java and XML. XML DOM. DOM Introduction DOM Nodes DOM Accessing DOM Node Info The <xsl:comment> element is used to create a comment node in the result� XPath is a language for finding information in an XML file. You can say that XPath is (sort of) SQL for XML files. XPath is used to navigate through elements and attributes in an XML document. You can also use XPath to traverse through an XML file in Java. XPath comes with powerful expressions that can be used to parse an xml document and

Java XPath Example - XPath Tutorial, Learn to apply xpath expressions to extract information from an XML file. This information can be XML nodes or XML attributes or even comments as well. Here is the same RDF graph using fewer of the fancy features available in the RDF/XML encoding. This is the same RDF graph, and thus the same OWL ontology. However, there is no owl:OntologyXML element here, and the XPath query will fail.

Using XPath Commands, XPath is an industry standard developed by the World Wide Web Consortium ( W3C). comment. Many of these elements are shown in the following sample XML, which Therefore, to access all CDs from the sample XML, use the following�

javax.xml.xpath (Java SE 11 & JDK 11 ), The XPath API supports XML Path Language (XPath) Version 1.0 convenience for retrieving the character data from a text node, attribute node, comment node, XPath provides access to the XPath evaluation environment and expressions.

  • I am using MSXML DOM ActiveX and this command seems to be failing... Any idea why?
  • No, I have no idea why. It was over 7 years since I last used the MSXML-libraries...
  • Sure your xpath root is ok? Try "//comment()" to make sure.
  • Or simply: /table/comment()[1]
  • There are probably all sorts approaches for this specific case but this was the closest I could get to a more general solution of finding the comment after a specific element.
  • I am using MSXML DOM ActiveX and this command seems to be failing... Any idea why?
  • Used this expression in my answer. Definitely +1
  • You'll probably get an error if there are comments in the root, so you might want to check that node.ParentNode is not null.
  • This isn't an answer. Ask your own question, with the program code you wrote.