Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

xml schema element name contains #

Status
Not open for further replies.

wyrdo

Technical User
Sep 19, 2013
4
US
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>
 
#" 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
[highlight #FCE94F] •Names cannot start with a number or punctuation character[/highlight]
•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:
 
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:
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
 
didn't even occur to me that the xml wasn't valid...
Thanks folks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top