views:

674

answers:

2

Hi,

using an in 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"&gt;
    <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?

Cheers
Jan

+3  A: 

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)".

John Saunders
+1 for precise meaning of "fixed"
KLE
+1  A: 

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

Mads Hansen
Thats it...Thanks Jan
Jan
+1 for nice answer. I checked the link you gave, it was useful. In your example, shouldn't the DOCTYPE line reference "foo" instead of "page". Wasn't it a cut'n-paste tipo?
KLE
@KLE you are correct, it was a copy/paste error. I have corrected the example.
Mads Hansen