Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is XML attribute valid?

Tags:

xml

I have a tag like this. In the attribute it contains space at the start. Is it right to give space in attribute? Please suggest on it.

<p id=" 10">space found at the starting point of attribute.</p>
<p id="10 ">space found at the end point of attribute.</p>
like image 883
parthi Avatar asked Feb 16 '16 05:02

parthi


People also ask

Which of the following is not a valid XML attribute?

The following are not valid XML names: <2do>—Names must not begin with a digit. <-name>—Names cannot start with special characters such as a hyphen or period. <x+2=4>—Names cannot contain special characters other than the period, hyphen, underscore, and colon.

Does XML have attributes?

XML elements can have attributes, just like HTML. Attributes are designed to contain data related to a specific element.

Is XML attributes must be quoted?

Attribute values must always be quoted It is illegal to omit quotation marks around attribute values. XML elements can have attributes in name/value pairs; however, the attribute value must always be quoted.


1 Answers

It is valid in general, but may not be in specific.

There are not many rules for what is allowed as a value. Without any specific restrictions based on the document type, attribute values must match the description of valid character data in the section Character Data and Markup of the xml specification. Essentially this says that the characters & and < are not allowed (but can be escaped with &amp; and &lt;). Some processors may balk on a > character as well, but this is allowed (except when it appears in ]]> in which case it must be escaped with &gt;). Any other data is allowed (with some escaping needed in some cases where quotation marks are mixed).

Now, depending on the application, there may be further restrictions on the values of attributes. For example, the ID type, must match the specification for a Name production. These must start with a letter (or one of a small number of punctuation characters) and continue with numbers, letters, or punctuation characters. In this case spaces would NOT be allowed.

There are addition possible restrictions covered in the specification (which may apply depending on the application), and further restrictions may be imposed by various schemas.

The comment by Kai Wu Toh provided a link to another question which covered the additional restrictions provided by HTML.


Links to the specification are provided in the answer, but to make the relevant sections easier to find if the links stop working, they are summarized here.

  1. Character Data and Markup is covered in section 2.4 of the XML 1.0 specification
  2. The ID validity constraint is covered in section 3.3.1 of the specification.
  3. The Name production is covered in section 2.3 of the specification.

The specification can be found at https://www.w3.org/TR/REC-xml.

An annotated version of the specification can be found at http://www.xml.com/axml/testaxml.htm.

like image 143
Matthew Avatar answered Sep 24 '22 02:09

Matthew