Comments in XML

Discussion in 'XML' started by Adam Badura, Apr 22, 2009.

  1. Adam Badura

    Adam Badura Guest

    Are comments in XML treated by parser (regarding the value of the
    enclosing element) as nothing or as a whitespace? In the example:

    <elem>some text<?-- a comment -->some more text</elem>

    the contents of "elem" is "some textsome more text", or maybe "some
    text some more text" or even "some text[unspecified whitespace]some
    more text"?

    How does it work for example with XPath when comparing element's value
    to an arbitrary string?

    I read the specification (
    however I am not entirely sure.

    Adam Badura
    Adam Badura, Apr 22, 2009
    1. Advertisements

  2. Adam Badura

    Adam Badura Guest

    <elem>some text<?-- a comment -->some more text</elem>

    Obviously I made a mistake in the comment - as it is not a comment.
    Proper example must look like:

    <elem>some text<!-- a comment -->some more text</elem>

    Adam Badura
    Adam Badura, Apr 22, 2009
    1. Advertisements

  3. XPath defines the string value of element nodes here:
    "The string-value of an element node is the concatenation of the
    string-values of all text node descendants of the element node in
    document order"
    so only the two text nodes "some text" and "some more text" constitute
    the string value of the element.
    Martin Honnen, Apr 22, 2009
  4. In other words, the value is the string "some textsome more text".
    Joe Kesselman, Apr 23, 2009
  5. I would guess is that all (serious) XML parsers treat comments as comments.

    Hermann Peifer, Apr 24, 2009
  6. Adam Badura


    May 15, 2010
    Likes Received:
    Parsers and comments


    A very quick test would have answered the question. try this xsl (uses built-in templates) on your xml file:

    Code (Text):
    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="...">
    The result is a firefox is exactly:
    some text some more text

    aljar, May 15, 2010
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.