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 (http://www.w3.org/TR/REC-xml/#sec-comments)
    however I am not entirely sure.

    Adam Badura
     
    Adam Badura, Apr 22, 2009
    #1
    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
    #2
    1. Advertisements

  3. XPath defines the string value of element nodes here:
    http://www.w3.org/TR/xpath#element-nodes
    "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
    #3
  4. In other words, the value is the string "some textsome more text".
     
    Joe Kesselman, Apr 23, 2009
    #4
  5. I would guess is that all (serious) XML parsers treat comments as comments.

    Hermann
     
    Hermann Peifer, Apr 24, 2009
    #5
  6. Adam Badura

    aljar

    Joined:
    May 15, 2010
    Messages:
    5
    Likes Received:
    0
    Parsers and comments

    Hello

    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="...">
    </xsl:stylesheet>
    The result is a firefox is exactly:
    some text some more text

    regards
     
    aljar, May 15, 2010
    #6
    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.