Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using - in XML element name

Tags:

Is it OK to use hyphen - in an XML element name?

e.g.

<rank-type>Rank</rank-type> 
like image 637
Thizzer Avatar asked Sep 25 '09 17:09

Thizzer


People also ask

Which is a correct XML element name?

XML Naming RulesElement names must start with a letter or underscore. Element names cannot start with the letters xml (or XML, or Xml, etc) Element names can contain letters, digits, hyphens, underscores, and periods. Element names cannot contain spaces.

What is the use of @XmlElement?

A JavaBean property, when annotated with @XmlElement annotation is mapped to a local element in the XML Schema complex type to which the containing class is mapped. Example 2: Map a field to a nillable element. Example 3: Map a field to a nillable, required element.

What is XmlElement?

The XML elements are the basic building block of the XML document. It is used as a container to store text elements, attributes, media objects etc. Every XML documents contain at least one element whose scopes are delimited by start and end tags or in case of empty elements it is delimited by an empty tag.

What is the difference between Xmlattribute and XmlElement?

An Attribute is something that is self-contained, i.e., a color, an ID, a name. An Element is something that does or could have attributes of its own or contain other elements.


2 Answers

As readily stated by Vinko and hacker, the answer is

Yes, dashes are allowed in names, whether for element names or attribute names. However a dash cannot be the first character in the name.

The W3C standard defines names as follows (section 2.3)

NameStartChar ::= ":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF] | [#x370-#x37D] | [#x37F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF] NameChar  ::=   NameStartChar | "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] | [#x203F-#x2040] Name  ::= NameStartChar (NameChar)* 

Ignoring the various extended ASCII charactes and double-byte characters (which starting with # sign in the syntax definition), a name needs to start with an upper or lower case letter (A-Z), a colon (:) or an underscore. The characters following this start character, if any, i.e. if the name is not a single character, can be any of the valid start characters and/or any of the digits (0-9), the famous dash (-) and the dot (.)

As pointed out in the other responses, where possible, it is advisable to avoid certain characters, even though they may be legal, lest we risk encountering some implementation specific difficulties with various parsers and interop layers.

The W3C itself has implicitly vindicated the use of dashes as these are extensively used in XSL for example.

like image 100
mjv Avatar answered Oct 04 '22 17:10

mjv


That's not an attribute name, but an element name.

In any case - is supported for both attribute and element names in the spec as long as the name doesn't start with it (<-foo> is not a valid element name).

Some software might choke on it though, so be warned. If you have to use such software try to find better software or to make it's developers support the XML spec properly.

W3CSchools' (polemic) recommendations

Make names descriptive. Names with an underscore separator are nice: <first_name>, <last_name>.

Names should be short and simple, like this: <book_title> not like this: <the_title_of_the_book>.

Avoid "-" characters. If you name something "first-name," some software may think you want to subtract name from first.

Avoid "." characters. If you name something "first.name," some software may think that "name" is a property of the object "first."

Avoid ":" characters. Colons are reserved to be used for something called namespaces (more later).

XML documents often have a corresponding database. A good practice is to use the naming rules of your database for the elements in the XML documents.

Non-English letters like éòá are perfectly legal in XML, but watch out for problems if your software vendor doesn't support them.

like image 21
Vinko Vrsalovic Avatar answered Oct 04 '22 19:10

Vinko Vrsalovic