6

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>
parthi
  • 734
  • 1
  • 4
  • 13
  • 5
    Yes, it is valid xml attribute. You can take your favorite programming language and try parse this xml or save this xml into file and open via browser. – Denis Feb 16 '16 at 05:38
  • You can read it here as well. http://stackoverflow.com/questions/70579/what-are-valid-values-for-the-id-attribute-in-html – Kai Wu Toh Feb 16 '16 at 05:39

2 Answers2

1

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.

Community
  • 1
  • 1
Matthew
  • 7,440
  • 1
  • 24
  • 49
0

XMLSpy says your XML is well-formed. So obviously no problem with leading blanks in attribute values!

Kim Homann
  • 3,042
  • 1
  • 17
  • 20