You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-dev@xerces.apache.org by Evert Haasdijk <ev...@zukkespijkers.nl> on 2001/07/12 12:45:10 UTC

Force use of a known DTD

Hi all,

I've got an application where I use XML documents that _must_ adhere to a
certain, known, DTD.
What I'd like to be able to do is force my DOMParser (or its Validator, or
whatever) to always use a DTDValidator with that DTD to validate documents,
even if their DOCTYPE is missing or says they're of a different type (if the
document is of a different type, I can't successfully use it)
I've looked closely at DTDValidator, but even creating my own validator
won't help, because its scanDocTypeDecl() method will only be called if a
<DOCTYPE entry is found.
Is there any way of achieving this?

TIA, Evert


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org


Re: Force use of a known DTD

Posted by Andrew Snare <AS...@allshare.nl>.
At 12:45 PM 12/07/2001 +0200, Evert Haasdijk wrote:
>I've got an application where I use XML documents that _must_ adhere to a
>certain, known, DTD.
>What I'd like to be able to do is force my DOMParser (or its Validator, or
>whatever) to always use a DTDValidator with that DTD to validate documents,
>even if their DOCTYPE is missing or says they're of a different type (if the
>document is of a different type, I can't successfully use it)
>I've looked closely at DTDValidator, but even creating my own validator
>won't help, because its scanDocTypeDecl() method will only be called if a
><DOCTYPE entry is found.
>Is there any way of achieving this?

I'm not sure there's a way to do what you say. However, the approach we use
where we need to force 1) a specific DTD and 2) validation is:

1) Use a DOCTYPE PUBLIC definition which uses a "public" identifier for the DTD
    you wish to use.
2) Check the doctype recorded in the DOM tree after the parse. If it's not
    correct, we turf it.
3) We attach an entity resolver to the parser that ignores all requests except
    for our public identifier from step 1). We then return our 'known' DTD.

This approach may be sufficient for your needs.

  - Andrew



---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org


Re: Force use of a known DTD

Posted by Alberto Massari <al...@exceloncorp.com>.
At 12.45 12/07/01 +0200, you wrote:
>Hi all,
>
>I've got an application where I use XML documents that _must_ adhere to a
>certain, known, DTD.
>What I'd like to be able to do is force my DOMParser (or its Validator, or
>whatever) to always use a DTDValidator with that DTD to validate documents,
>even if their DOCTYPE is missing or says they're of a different type (if the
>document is of a different type, I can't successfully use it)
>I've looked closely at DTDValidator, but even creating my own validator
>won't help, because its scanDocTypeDecl() method will only be called if a
><DOCTYPE entry is found.
>Is there any way of achieving this?

The code I use for this task is this (but I use it to validate XML files 
that don't have a DOCTYPE entry, so I am not sure it works if there is one):

XMLByte* dummyXML="<?xml version=\"1.0\"?><!DOCTYPE myDtdRoot SYSTEM 
\"MyFixedDTD.dtd\"><myDtdRoot/>";
MemBufInputSource inputSource(dummyXML,strlen(dummyXML),"dummy",false);
SAXParser sp;
sp.setDoValidation(true);
sp.parse(inputSource);

// now, reuse the validator and parse the new doc
FileInputSource inputSource2("somefile.xml");
sp.parse(inputSource2, true);

Hope this code is really working (as I removed a lot of extra stuff to get 
the core of the routine) and it helps you,

Alberto

-------------------------------
Alberto Massari
eXcelon Corp.
http://www.StylusStudio.com


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org