XML Attribute

XML > Structure > XML Attribute

An XML Element can contain 0-n XML Attributes. Attributes are contained within the start tag and must be unique within it.

In practice this means an XML element can look like these examples

<MyElement myAttribute="attribute value"/>
<MyElement myAttribute="attribute value">...</MyElement>
<MyElement myAttribute='attribute value'>...</MyElement>
<MyElement myAttribute1="attribute value" myAttribute2="attribute value">...</MyElement>

Namespaces

Attributes can be associated with a namespace. Namespaces allow data to be broken up, it also allows a parser to ignore parts of a document it was not designed to deal with.

Namespaces are applied to an attribute using a namespace prefix. The prefix must be defined within the containing element (or any of its contianing parent elements back to the document element).

<RootElm xmlns:nsA="MyNamespace">
    <SubElm>
        <MyElement nsA:myAttribute1="attribute value" myAttribute2="attribute value">...</MyElement>
    </SubElm>
</RootElm>

Typically attributes that have no namespace prefix, are deemed to belong to the containing elements namespace. But this is a complex area, have a look at XSD Namespace rules.

Namespaces are a concept defined in a separate W3C specification 'Xml Names' the XML 1.0 & 1.1 specifications allows for namespaces, but do not make use of them explicitly. Other schema descriptions (notably XSD) make extensisve use of namespaces.

Special Characters

If you put control characters (",',&) into an attributes value, this would cause the parser to miss understand the resulting text, so in order for the parser to operate correctly these control characters need to be escaped, see Escaping XML Data.

XML Comments

You can not place a comment inside an attribute value.

Whitespace

When an XML Parser reads an XML Attribute it is supposed to do some processing to normalize the whitespace within the attribute value.

Normalization for all Attributes 

When an XML Parser reads an XML Attribute the XML Spec says it should normalize the whitespace it contains (this means replacing all whitespace characters with spaces).

3.3.3 Attribute-Value Normalization

Before the value of an attribute is passed to the application or checked for validity, the XML processor MUST normalize the attribute value by applying the algorithm below, or by using some other method such that the value passed to the application is the same as that produced by the algorithm.

  1. All line breaks MUST have been normalized on input to #xA as described in 2.11 End-of-Line Handling, so the rest of this algorithm operates on text normalized in this way.
  2. Begin with a normalized value consisting of the empty string.
  3. For each character, entity reference, or character reference in the unnormalized attribute value, beginning with the first and continuing to the last, do the following:
    • For a character reference, append the referenced character to the normalized value.
    • For an entity reference, recursively apply step 3 of this algorithm to the replacement text of the entity.
    • For a white space character (#x20, #xD, #xA, #x9), append a space character (#x20) to the normalized value.
    • For another character, append the character to the normalized value.

Additional Normalization rules for DTDs

When the XML document is ready in conjunction with a DTD further rules come into play.

3.3.3 Attribute-Value Normalization

If the attribute type is not CDATA, then the XML processor MUST further process the normalized attribute value by discarding any leading and trailing space (#x20) characters, and by replacing sequences of space (#x20) characters by a single space (#x20) character.

Additional Normalization rules for XSD's

When the XML document is read in conjunction with an XSD schema, a number of additional rules apply.

If the Attributes data type is "xs:string" no further whitespace normalization is performed.

If the Attributes data type is "xs:normalisedString" the xsd "Replace" rule is applied (which should already have been applied as part of the XML attribute normalization).

Attributes of any other data type are normalized using the xsd "Collapse" rule.

If a custom data type is defined within the XSD that is based on "xs:string" or "xs:normalisedString", the the XSD can change the whitespace rules for that data type using the whitespace facet.

preserve
    No normalization is done, the value is not changed (this is the behavior required by [XML] for element content)
replace
    All occurrences of #x9 (tab), #xA (line feed) and #xD (carriage return) are replaced with #x20 (space)
collapse
    After the processing implied by replace, contiguous sequences of #x20's are collapsed to a single #x20, and any #x20 at the start or end of the string is then removed.

Note not all XML parsers correctly implement the whitespace normalization rules.

EBNF Syntax

The syntax for an attribute is described by the W3C as using EBNF as follows.

[10]     AttValue                ::=   '"' ([^<&"] | Reference)* '"' 
| "'" ([^<&'] | Reference)* "'" [40] STag ::= '<' Name (S Attribute)* S? '>' [41] Attribute ::= Name Eq AttValue [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>'

See Also

Try Liquid XML Free and see how we can help you today Free Trial