|
Summary
To get entity name support you need to use a DTD, however you can get a similar effect using XML elements and a Schema.
Recently I bumped into the following problem:
I was working with a set of XML-files that referenced to a complex DTD file. Since, in that application, I rather use schemas I rewrote the DTD and “made it into†a schema. However, I made one mistake; I forgot to check if entities are supported in schemas as well as in DTD’s. Pretty soon I discovered that wasn’t the case. My —, – and … stayed undeclared and that made the use of my application impossible.
The big question is: do you have to use a DTD to get entity name support in an XML-file? The simple answer is: yes, the only way is to have either an internal or external DTD and declare the entities within it.
XML provides entities that are named fragments of content that can be used in the construction of DTD's and instance documents. But isn’t Schemas supposed to make DTD’s obsolete? No. The purpose of W3C is not to replace the DTDs with the WC3 XML Schema; DTDs include 4 features:
1) Macro instructions 2) Inclusions 3) Linking 4) Schema
And the purpose of W3C XML Schema is to replace item 4. Hence, DTDs are here to stay for, at least, a foreseeable future.
But if I want to use a schema and not a DTD, how can I get the equivalent of entity name support in my XML-files? I’ll show you below.
toc.xml contains a short table of contents containing the entities ‐ and …. If we should use a DTD to declare the entities it would look like this:
toc.xml
<?xml version=1.0?> <!DOCTYPE note SYSTEM toc.dtd>
<toc title=Savy Articles> <item identifier=1>Managing the Whitespace – An Art</item> <item identifier=2>Strong Sales by Focus on Training</item> <item identifier=3>The For Each…Next Loop</item> </toc>
Here, we declare the entities as in an external DTD, and we reference the entities in the content of the first and third item element. The DTD looks like this:
toc.dtd
<!ELEMENT toc (item)> <!ELEMENT item (#pcdata)>
<!ATTLIST toc title cdata> <!ATTLIST item identifier cdata>
<!ENTITY ndash - > <!ENTITY hellip ...>
Luckily for those developers that want to work only with schemas, we can achieve a similar outcome by declaring an element in a schema and setting the element’s content as follows:
tocschema.xml
<?xml version=1.0 encoding=iso-8859-1?>
<Schema name=toc xmlns=urn:schemas-microsoft-com:xml-data xmlns:dt=urn:schemas-microsoft-com:datatypes>
<AttributeType name=title required=yes dt:type=string/> <AttributeType name=identifier required=yes dt:type=string/>
<ElementType name=toc dt:type=string> <attribute type=title/> <element type=item minOccurs=1 maxOccurs=*/> </ElementType>
<ElementType name=item dt:type=string> <attribute type=identifier/> </ElementType>
<dt:element name=ndash type=dt:token fixed= - /> <dt:element name=hellip type=dt:token fixed=.../>
</Schema>
And, instead of entities, we use elements in the XML-file:
<toc title=Savy Articles xmlns:dt=x-schema:tocschema.xml> <item identifier=1>Managing the Whitespace <dt:ndash/> An Art</item> <item identifier=2>Strong Sales by Focus on Training</item> <item identifier=3>The For Each <dt:hellip/> Next Loop</item> </toc>
In this case the schema processor will process the item element and the ndash and hellip elements separately and for the ndash and hellip element simply supply “ – “ for the former and “…†for the latter.
Thanks you for your time!
Robert Kviby Binary Bros - FREE BOOKS AND ARTICLES ON NGCODE.COM!
|