Search code examples
xmlxpathxquery

How is an empty XML element represented?


Let's say I have the following XML:

<Person>
    <Name>Tom</Name>
    <Age/>
</Person>

Is the correct representation of the Age value the empty string, '' or a null value, such as NULL, None, nil, etc. For example doing in python it gives None, but is this part of the standard or a serialization decision?

>>> repr(etree.fromstring('<Person><Name>Tom</Name><Age/></Person>')
        .xpath('/Person/Age')
        [0].text)
# 'None'

Solution

  • At the XML level, an empty element simply has no content.

    In XPath 1.0, /Person/Age/node() returns an empty node-set; in XPath 2.0+, an empty sequence.

    The None in your Python example is an artifact of Python, not XML or XPath. The /Person/Age XPath expression returns a Python list containing an Age element. That the element's (Python) .text attribute has a value of None is about Python, not XML or XPath.