Can XML attributes have spaces?
Asked Answered
L

1

14

I'm creating a DTD for an xml document. I have an Enumerated attribute for an xml element. My question is: Can the attribute Type have spaces?

eg:

  <!ELEMENT Link (#PCDATA)>
  <!ATTLIST Link Type (Amendment|Reference|Superseded|Modified|
  Corrigendum|Corresponds|Endorsement|Equivalent|Identical|Modified|
  Not Equivalent|Note taken of|Related|Similar) "Reference">

So what I would like is:

  <Link Type="Not Equivalent" \>

But that seems to barf. When I say barf, I mean that when I try to validate the document (Eg Open it in a web browser) I get an error message:

Invalid character found in ATTLIST enumeration. Error processing resource 'file:///C:/myxmldocument.xml'. ...

Is there some magic voodoo I need to do for spaces? Or is it just tough luck?

I looked in a few spots but couldn't see any reference

Lamdin answered 16/2, 2010 at 23:50 Comment(1)
If you're in the process of creating the DTD yourself, then I seriously suggest reconsidering, and writing an XML Schema instead. It's an awful lot more flexible, while at the same time being more specific about what is and isn't allowed.Calamitous
S
15

The short answer is no. Xml attributes are name tokens and name tokens cannot contain spaces. The relevant specs are linked:

Attributes

Attribute Types (see NotationType)

Names (See 4a - NameChar)

The invalid character is the space. You can use '.' or '-' as a separator if you like.

Staging answered 16/2, 2010 at 23:59 Comment(2)
XML itself supports spaces in attributes just fine - it's the DTD spec that doesn't permit it in ATTLIST enumerations.Calamitous
Just to clarify - spaces in attribute Names are always invalid. Spaces in attribute Values are OK, BUT an enumerated attribute declaration requires that all tokens in the enumeration must be sequences of Name Characters only, so no spaces. You could use 'Not_Equivalent (or even '-Equivalent' is OK, although this isn't a valid Name).Brander

© 2022 - 2024 — McMap. All rights reserved.