INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

xml schema element name contains #

xml schema element name contains #

(OP)
I'm trying to put together an XSD schema to represent the XML required to feed into a program that generates a test. I've done ok so far, however some of my complex types contain elements with name="#comment". This is not going over well, as apparently # is not an acceptable character for a element name.

Is there a way to escape out the # or represent it with, perhaps, an html or ascii code? I have pasted sample xml and sample of the unhappy complex type below.

I ask because the code has been in use a while and it would be problematic to change it to support, for instance name="comment"

Sample XML

CODE -->

<main>
   <Window>
      <Data><Var name="varname">varval</Var></Data>
      <#comment>This var represents the variable name and value</#comment>
   </Window>
</main> 

Sample Schema

CODE -->

<xs:complexType name="WindowType">
   <xs:choice minOccurs="0" maxOccurs="unbounded">
      <xs:element name="Data" type="dataElementType" />
      <xs:element name="#comment" type="emptyType" />
   </xs:choice>
</xs:complexType> 

RE: xml schema element name contains #

"#" as a first character for an element name constitutes an illegal name.

From W3Schools website:
"XML Naming Rules
XML elements must follow these naming rules:
•Names can contain letters, numbers, and other characters
•Names cannot start with a number or punctuation character
•Names cannot start with the letters xml (or XML, or Xml, etc)
•Names cannot contain spaces
Any name can be used, no words are reserved."

From the XML Standard:[4] 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]

Links:
http://www.w3.org/TR/2008/REC-xml-20081126/#NT-Nam...
http://w3schools.com/xml/xml_elements.asp

RE: xml schema element name contains #

Welcome to Tek-Tips.

The XML-like fragment you have called 'Sample XML' is not XML. The XML specification itself excludes the character #, along with most other symbol characters in the ASCII set, from names and gives the reason for doing so.

Now, there is a way to provide a comment in XML. Your sample rendered as XML might be:

CODE --> XML

<main>
   <Window>
      <Data><Var name="varname">varval</Var></Data>
      <!-- This var represents the variable name and value -->
   </Window>
</main> 

An XML comment is a node that will be represented in the XML Document Object Model (DOM) after the XML is parsed. I am not aware of a means to specify the requirement for an XML comment using XML Schema, but one could easily enforce the requirement for an XML comment in an XML aware application.

From your expressed reluctance to change code which is 'working' my guess is that the code is not using an XML processor at all, but rather has some simplistic code that purports to process XML. I would encourage the use of one of the several available (many free) XML processors rather than rolling your own.

I would also point out that, by having this non-XML feature, you will have to forego virtually every advantage obtained by the use of XML.

Finally, congratulations on using XML Schema. Beyond exposing this flaw in your test generator, you are creating documentation that will be most useful either to you in a few months, or to the next person that must take up this code. And, there are some excellent tools on the market that can render your schema in a graphical manner, aiding understanding.

Tom Morrison
Hill Country Software

RE: xml schema element name contains #

(OP)
didn't even occur to me that the xml wasn't valid...
Thanks folks.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close