Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DTD Entities vs XML-Schema Elements

Using an Document DTD I did the following:

file.xsl:

<!DOCTYPE xsl:stylesheet[
  <!ENTITY red "rgb(255,0,0)">
]>

<xsl:stylesheet>
   [...]
   <xsl:attribute name="color">&red;</xsl:attribute>
   [...]
</xsl:stylesheet>

I wanted to change everything to XML-Schema. So I tried:

file.xsd:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="red" type="xs:token" fixed="rgb(255,0,0)" />
</xsd:schema>

file.xsl:

<xsl:stylesheet
    xmlns:defs="http://www.w3.org/2001/XMLSchema-instance"
    defs:noNamespaceSchemaLocation="file.xsd">

    [...]
    <xsl:attribute name="color"><defs:red/></xsl:attribute>
    [...]
</xsl:stylesheet>

Now parsing the file via Xalan red is not translated like in the DTD version. Where is my error? Are Schema files not read during the parsing process?

like image 896
Jan Avatar asked Jul 29 '09 23:07

Jan


People also ask

What are the differences between DTD and XML Schema?

XML schemas are written in XML while DTD are derived from SGML syntax. XML schemas define datatypes for elements and attributes while DTD doesn't support datatypes. XML schemas allow support for namespaces while DTD does not. XML schemas define number and order of child elements, while DTD does not.

What is XML DTD elements?

The purpose of a DTD is to define the legal building blocks of an XML document. It defines the document structure with a list of legal elements. A DTD can be declared inline in your XML document, or as an external reference.

How elements and entity are declared in a DTD?

If an entity is declared within a DTD it is called as internal entity. entity_name is the name of entity followed by its value within the double quotes or single quote. entity_value holds the value for the entity name.


2 Answers

The fixed attribute in an element definition does not tell a parser to do a text substitution. It simply means that the value of the element must always be the same.

In fact, I believe your XSLT is producing XML that will not validate against your schema, since the value of the <defs:red> element is not "rgb(255,0,0)". Even if you used default instead of fixed, this is not a text substitution. It just means that, if no value is specified, when the value is queried in a DOM instance, you'll find the value set to "rgb(255,0,0)".

like image 176
John Saunders Avatar answered Sep 28 '22 09:09

John Saunders


Using schemas to declare your structural rules (rather than DTD) does not preclude you from using entities.

The manner in which you use entity references to substitute the declared content does not change.

Entity references do not need to be declared only within DTD files. You can declare them inline in your XML files.

<?xml version="1.0"?>
<!DOCTYPE foo [
  <!ENTITY red "rgb(255,0,0)">
]>
<foo>
  <bar color="&red;" /> 
</foo>

http://www.ibm.com/developerworks/xml/library/x-tipentref.html

like image 39
Mads Hansen Avatar answered Sep 28 '22 09:09

Mads Hansen