• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Attribute name

 
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I have a few problems regarding XML's syntax. Are attribute names allowed to have space in between(ie. more than one word, like first color="red")? If so, how can a parser tell where the element name ends and where attribute name begins?
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Cameron,
According to Teach YourSelf XML p192 attribute names can include spaces; element names cannot.
Think the parser takes anything between the start of a tag '<' and the first space to be an element name; anything between the first space and the '=' as an attribute name.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Cameron Park
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Jane.
 
Leverager of our synergies
Posts: 10065
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hm, according to Beginning XML "The same rules apply to naming attributes as apply to naming elements". If you try to write an attribute name with space, you will get an error message.
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh no Map! Do I have a bad source reference?? I haven't had time to actually try it. Did you run some code? Did it give you an error??
Thanks
Jane
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Attribute, and their values, are attached to element, so it has to follow the naming rule with element. Such as beginning with a letter or a underscore, you will receive errors on $Priority, 2Choice.... as attribute name.
Moreover, you can allow to have only ONE attribute name with a given name of element. It doesn't behave like element to be able to have multi-occurence. Ex:
<Items>
<Item> ......</Item>
<Item> ......</Item>
<Items>
enoc
 
Mapraputa Is
Leverager of our synergies
Posts: 10065
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

There were a lot of questions and doubts regarding value of certification (SCJP primarily), but what is unquestionable, that Certified bring discussion to the higher level. Thanks, Enoc!
Jane, you should write to Teach YourSelf XML people and tell them about their mistake! They have to mention you in their next edition!
Yes, I tried to code an attribute with a space in it�s name and checked it with XML Spy (I believe it uses MSXML) and with Xalan (which I believe uses Xerces parser).
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Enoc, Map.
 
Cameron Park
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So element names can not contain spaces either?
 
Mapraputa Is
Leverager of our synergies
Posts: 10065
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's correct.
 
Mapraputa Is
Leverager of our synergies
Posts: 10065
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
More detailed.
In XML name:
first symbol= a letter OR _ OR :
( �:� symbol is not recommended because of possible conflict with namespaces). Of course, letter can be from any language supported by Unicode.
So practically we can start our name either with letter or _
myName or
_myName
any other symbols: letter OR . OR - OR _ OR : OR <digit> OR <CombiningChar> OR <Extender>
where <digit> can be any digit from 0-9 diapason + digits from other languages: Arabic-Indic Digits set, Bengali Digits, Tamil Digits, Tibetian Digits...
<CombiningChar> - special character such as accents and tilde, can be used in other languages.
<Extender> = even more exotic symbols, such as �vertical Kana Repeat Mark�, for example
If to simplify picture and restrict us with English characters, all we can have is
my_Name
my.Name1
_my-Name2
no spaces, no punctuations besides . - _
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In cases like this where there is some doubt, don't refer to somebody's book - refer to the specification.
The XML 1.0 specification section 3.3 has this:
Attribute-list Declaration
[52] AttlistDecl ::= '<!ATTLIST' S Name AttDef* S? '>'
[53] AttDef ::= S Name S AttType S DefaultDecl

" ... The Name in the AttlistDecl rule is the type of an element. At user option, an XML processor may issue a warning if attributes are declared for an element type not itself declared, but this is not an error. The Name in the AttDef rule is the name of the attribute."
Since "Name" is used in both productions, both elements and attributes must be constructed according to the same rules. The rules are given in section 2.3:
Names and Tokens
[4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' | CombiningChar | Extender
[5] Name ::= (Letter | '_' | ':') (NameChar)*

So whitespace is not allowed in either element names or attribute names.


------------------
Phil Hanna
Author of :
JSP: The Complete Reference
Instant Java Servlets
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic