w3resource

XML attribute

Attribute value

Any attribute value must consist of one of the following types of attributes available to an XML document.

  • CDATA
  • ENTITY
  • Enumeration
  • ID
  • IDREF
  • IDREFS
  • NMTOKEN
  • NMTOKENS
  • NOTATION

Attribute syntax

<sometext>
<![CDATA[ They're saying "x < y" & that "z > y" so I guess that means that z > x ]]>
</sometext>

For all CDATA, the character literals which are prohibited, are not characters which are not literals and since they are not, it causes problems for exporting data in the form of other datasets like executing Processing Instructions.

Example of Built-in entity in attribute content:

<say hello word='&apos;Hi&apos;' />

Use of the built-in entity &apos; inside attribute content

Example of CDATA sections

<sometext>
<![CDATA[ They're saying "x < y" & that "z > y" so I guess
that means that z > x ]]>
</sometext>

For all CDATA, the character literals which are prohibited, are not characters which are not literals and since they are not, it causes problems for exporting data in the form of other data sets like executing Processing Instructions.

XML Quotation marks for attribute values

Attribute values must be enclosed in quotation marks. In html, attribute values don't have to be in quotes for a browser to present a document. But that does not work for xml. If quotes are removed, an xml parser will generate an error. Its your choice to use a single or a double quote, but you need to be consistent in using them. The following example will tell you what is wrong and what is correct:

These are the correct use of quotes:

<tutorial type="text">
<tutorial type='text'>

But this is a wrong use of quote:

<tutorial type="text'>

Attribute and namespace

prefix:localname='value'
prefix:localname="value"

Attribute names are QNames. The namespace of an attribute with a given prefix is the namespace specified by the in-scope namespace declaration for that prefix. It is an error if no such namespace declaration is in scope. Unprefixed attributes are not in any namespace even if a default namespace declaration is in the scope.

Attributes are required to be prefixed by a namespace to remain unique in an XML document reference.

Example of qualified attributes:

<Person xmlns='urn:w3resource.com:People'
xmlns:b='urn:w3resource.com:People:base' 
xmlns:u='urn:w3resource.com:units' >
<name>Martin</name>
<age b:base='10' u:units='years' >33</age>
</Person>

An attribute with a local name of base in the namespace urn:w3resource.com:People:base and an attribute with a local name of units in the namespace urn:w3resource.com:units

Example of unqualified attributes

<Person xmlns='urn:w3resource.com:People' >
<name>Martin</name>
<age base='10' units='years' >33</age>
</Person>

Previous: XML namespace
Next: XML processing instructions (PI), comments, whitespaces



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/xml/attribute.php