Using - in XML element name
Asked Answered
A

3

25

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

e.g.

<rank-type>Rank</rank-type>
Amazonas answered 25/9, 2009 at 17:5 Comment(4)
I think it's valid to do so, but I know for a fact that it makes the SimpleXML parser in PHP unhappy when you try to reference nodes like $node->child-node.Capitalize
inkedmn, I'd expect it to make php parser unhappy, not SimplXML parser ;-) Because, it's effectively $node->child - node.Disused
You can access it like $node->{'child-node'}.Milagrosmilam
Related for @: #7066193Kneepan
E
39

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.

Enmesh answered 25/9, 2009 at 17:32 Comment(0)
E
10

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.

Ellene answered 25/9, 2009 at 17:8 Comment(14)
So, <first_name /> is a nice element name and <xsl:value-of /> is not?Disused
They are both valid. I'm assuming that the question may have a cause in some incident he's had with a -. That cause might be faulty software.Ellene
Vinko, I said "nice", not "valid". I think it's funny when w3cschools accuse w3c of using worst practices ;-) And personally for œsthetic reasons, I'm on w3c side.Disused
I've now clarified the meaning.Ellene
I personally find no element or attribute name "nice". I mind only about validity and practicality. And it may be impractical if you are stuck with a lousy library.Ellene
It was not your fault, anyway. But I wouldn't link this resource and would call it as a sensible advice (as long as it's explained), but never best practice.Disused
Vinko, the word "nice" is taken from your quote. But wouldn't being stuck with lousy library by impractical? ;-)Disused
So the best practice in the case of having a lousy tool is to ignore reality and use the damned dash anyway? (You can't always change or fix the tools)Ellene
Go talk to the authors of the quote about the names' niceness or lack thereof :).Ellene
Vinko, you represent them ;-) And no, I think the best practice would be first to see if you have lousy tools, then to see if you can change it and then submit to using awful underscores all over the place ;-)Disused
Okay, that makes sense. Will adjust the answer.Ellene
But you never again say I represent them. I merely quoted them.Ellene
That's how you took responsibility. But true, adjusting the link's wording does change things.Disused
+1 to make up for a rather unfair -1. Your advice was sensible if not fully [pedentically] exacting...Enmesh
D
5

Yes, it is. But your example shows element name, not attribute-name. Which is also valid.

Disused answered 25/9, 2009 at 17:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.