1

I have a API with the following validation scheme:

<root>
<header>
</header>
<body>
<element></element>
</body>

'element' has the rule (in the xsd schema): with the restriction base: xs:string

is it possible to use DTD entities without changing the rules? Like this:

<!ENTITY foo "something cool">
<element>&foo;</element>

So far the scheme validation fails when I use an entity. When I don't (using a string) it works fine.

Any ideas?

edit: the <!ENTITY foo "something cool"> part works fine (I've tested it with external entities). I just can't use the result.

C. M. Sperberg-McQueen
  • 24,596
  • 5
  • 38
  • 65
f4der
  • 711
  • 4
  • 18

3 Answers3

3

Nothing in the XSD spec forbids the use of a DTD and entity declarations in DTD notations for the document to be validated by an XSD validator. (It can't: XSD operates on any well-formed XML infoset, after entity expansion, so XSD can't really tell whether there has been DTD processing or not.) So in principle the answer is "yes, it's possible".

The co-existence of DTDs and XSD schemas does tend to confuse some software and some people, however, and there may be processors which will assume, by default, that if you have a document type declaration in the input, then you must not want XSD validation. (I haven't encountered any, but that doesn't mean they don't exist.) You may be able to override that default assumption.

C. M. Sperberg-McQueen
  • 24,596
  • 5
  • 38
  • 65
  • Thank you for your answer. In my case the XML was (already) valid, but the validator fails. – f4der Jun 04 '14 at 08:27
1

Complementing the answer by @CMSperbergMcQueen with an example.

I tested your example with this XML:

<!DOCTYPE root [
    <!ENTITY foo "something cool">
]>

<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:noNamespaceSchemaLocation="dtdschema.xsd">
    <body>
        <element>A plain string</element>
        <element>&foo;</element>
    </body>
</root>

and an XSD element declaration like this one:

<xs:element name="element">
    <xs:simpleType>
        <xs:restriction base="xs:string"></xs:restriction>
    </xs:simpleType>
</xs:element>

It validated successfully in Xerces and Saxon EE. You can also see it validating it in this online service where you can modify it and see the results.

helderdarocha
  • 23,209
  • 4
  • 50
  • 65
  • Thank you for the validator and the example. It was helpful! In my case the XML was (already) valid, but the validator fails. – f4der Jun 04 '14 at 08:28
0

I finally discovered that the problem wasn't a invalid XML, but the PHP LibXML validator (DOMDocument::schemaValidate) fails. Validating the XML example of helderdarocha (which is valid) results in a schema validation error:

Internal error: xmlSchemaVDocWalk, there is at least one entity reference in the node-tree currently being validated. Processing of entities with this XML Schema processor is not supported (yet). Please substitute entities before validation..

So in this case my XML was correct (all the time), but the validator fails to validate it.

Hope this will save some people a headache :)

f4der
  • 711
  • 4
  • 18