Pregunta

Lets say I have a snippet of a DTD declaration like this.

<!ELEMENT book ANY>
<!ATTLIST book genre #FIXED "fantasy">

Note that the genre attribute was declared with a fixed default value of fantasy.

What exactly is the meaning of such a declaration? Two possible interpretations come to mind:

  1. A document is not valid unless each book element contain a genre attribute with value fantasy.
  2. A document is valid if each book element either contains a genre attribute with value fantasy or does not contain the genre attribute at all.

I did not find a definitive answer in the DTD specification, even though the second one seems more likely because of the following part:

Validity constraint: Fixed Attribute Default

If an attribute has a default value declared with the #FIXED keyword, instances of that attribute MUST match the default value.

¿Fue útil?

Solución

Interpretation 2 is the correct one. Demonstration:

DTD (fixed.dtd)

<!ELEMENT root ANY>

<!ELEMENT book ANY>
<!ATTLIST book id ID #IMPLIED
               genre CDATA #FIXED "fantasy">

<!ELEMENT magazine ANY>
<!ATTLIST magazine id ID #IMPLIED>

XML 1

<!DOCTYPE root SYSTEM "fixed.dtd">
<root>
  <book id="ID01" genre="fantasy"/>
  <magazine id="ID02" />
</root>

xmllint output:

$ xmllint --postvalid fixed.xml
<?xml version="1.0"?>
<!DOCTYPE root SYSTEM "fixed.dtd">
<root>
  <book id="ID01" genre="fantasy"/>
  <magazine id="ID02"/>
</root>

XML 2

<!DOCTYPE root SYSTEM "fixed.dtd">
<root>
  <book id="ID01"/>
  <magazine id="ID02" />
</root>

xmllint output:

$ xmllint --postvalid fixed.xml
<?xml version="1.0"?>
<!DOCTYPE root SYSTEM "fixed.dtd">
<root>
  <book id="ID01"/>
  <magazine id="ID02"/>
</root>

XML 3

<!DOCTYPE root SYSTEM "fixed.dtd">
<root>
  <book id="ID01" genre="crime"/>
  <magazine id="ID02" />
</root>

xmllint output:

$ xmllint --postvalid fixed.xml
<?xml version="1.0"?>
<!DOCTYPE root SYSTEM "fixed.dtd">
<root>
  <book id="ID01" genre="crime"/>
  <magazine id="ID02"/>
</root>
fixed.xml:3: element book: validity error : Value for attribute genre of book is different from default "fantasy"
fixed.xml:3: element book: validity error : Value for attribute genre of book must be "fantasy"
Document fixed.xml does not validate

Otros consejos

The second one is true! In effect the attribute can not declarate in XML.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top