Search code examples
xmlxpathdatamodelxmlnode

Is the name->value pair of an attribute in XML considered to be two nodes, interpreted by XPATH?


The following article http://docs.oracle.com/javaee/1.4/tutorial/doc/JAXPXSLT3.html says that XPATH considers the following to be nodes:

  • Root
  • Element
  • Text
  • Attribute
  • Comment
  • Processing instruction
  • Namespace

What I want to know is whether an attribute 'id' and its value '2' are considered to be two different nodes? e.g. <name id="2">text</name>

Or are text nodes just the 'data' http://www.w3.org/XML/Datamodel.html between elements? My intuition tells me that XPATH would consider there to be 4 nodes in this example - the 'name' element, the attribute 'id' the text value '2' and the text value 'text'.


Solution

  • An attribute node has a name and a value. They are not separate nodes. The following XML:

    <name id="2">text</name>
    

    ...represents three nodes: 1) an element whose name is name; 2) a child node of name that is a text node; 3) an attribute node whose name is id and whose value is 2.

    From the spec:

    An attribute node has an expanded-name and a string-value.

    And later:

    An attribute node has a string-value. The string-value is the normalized value as specified by the XML Recommendation [XML]. An attribute whose normalized value is a zero-length string is not treated specially: it results in an attribute node whose string-value is a zero-length string.