Specifying a DTD for a DOMDocument

Specifying a DTD for a DOMDocument

am 23.10.2007 11:22:10 von bmichel

Hey,

I'd like to specify the DTD file for a DOMDocument on creation; so I
can validate it.
The DTD file already exists (let's call it content.dtd)

$dom = new DOMDocument('1.0', 'utf-8');
// Do something here to link cotnent.dtd to the DOMDocument. How do I
do that?

// Form the xml document.
$dom->createElement('root_node');
....


I found through the php documentation that I can validate xml content
against a DTD if load the XML file (but not if I create it). Something
like that (assuming the xml content is in file content.xml which
specifies **):

$dom = new DOMDocument;
$dom->Load('content.xml');
if ($dom->validate()) {
// document is valid
}

Re: Specifying a DTD for a DOMDocument

am 23.10.2007 15:13:54 von zeldorblat

On Oct 23, 5:22 am, bmic...@gmail.com wrote:
> Hey,
>
> I'd like to specify the DTD file for a DOMDocument on creation; so I
> can validate it.
> The DTD file already exists (let's call it content.dtd)
>
> $dom = new DOMDocument('1.0', 'utf-8');
> // Do something here to link cotnent.dtd to the DOMDocument. How do I
> do that?
>
> // Form the xml document.
> $dom->createElement('root_node');
> ...
>
> I found through the php documentation that I can validate xml content
> against a DTD if load the XML file (but not if I create it). Something
> like that (assuming the xml content is in file content.xml which
> specifies **):
>
> $dom = new DOMDocument;
> $dom->Load('content.xml');
> if ($dom->validate()) {
> // document is valid
>
> }

See this:

createdocumenttype.php>

Re: Specifying a DTD for a DOMDocument

am 23.10.2007 15:34:56 von bmichel

On Oct 23, 3:13 pm, ZeldorBlat wrote:
> On Oct 23, 5:22 am, bmic...@gmail.com wrote:
>
>
>
> > Hey,
>
> > I'd like to specify the DTD file for a DOMDocument on creation; so I
> > can validate it.
> > The DTD file already exists (let's call it content.dtd)
>
> > $dom = new DOMDocument('1.0', 'utf-8');
> > // Do something here to link cotnent.dtd to the DOMDocument. How do I
> > do that?
>
> > // Form the xml document.
> > $dom->createElement('root_node');
> > ...
>
> > I found through the php documentation that I can validate xml content
> > against a DTD if load the XML file (but not if I create it). Something
> > like that (assuming the xml content is in file content.xml which
> > specifies **):
>
> > $dom = new DOMDocument;
> > $dom->Load('content.xml');
> > if ($dom->validate()) {
> > // document is valid
>
> > }
>
> See this:
>
> > createdocumenttype.php>

I tried this approach. The xml content is now formatted the way I want
to.
The problem though is that $dom->validate() is returning false
although the xml content is valid with respect to the DTD.
Seems that the validate() function only works when loading xml files.
Any thoughts?

Re: Specifying a DTD for a DOMDocument

am 23.10.2007 16:55:51 von shimmyshack

On Oct 23, 2:34 pm, bmic...@gmail.com wrote:
> On Oct 23, 3:13 pm, ZeldorBlat wrote:
>
>
>
>
>
> > On Oct 23, 5:22 am, bmic...@gmail.com wrote:
>
> > > Hey,
>
> > > I'd like to specify the DTD file for a DOMDocument on creation; so I
> > > can validate it.
> > > The DTD file already exists (let's call it content.dtd)
>
> > > $dom = new DOMDocument('1.0', 'utf-8');
> > > // Do something here to link cotnent.dtd to the DOMDocument. How do I
> > > do that?
>
> > > // Form the xml document.
> > > $dom->createElement('root_node');
> > > ...
>
> > > I found through the php documentation that I can validate xml content
> > > against a DTD if load the XML file (but not if I create it). Something
> > > like that (assuming the xml content is in file content.xml which
> > > specifies **):
>
> > > $dom = new DOMDocument;
> > > $dom->Load('content.xml');
> > > if ($dom->validate()) {
> > > // document is valid
>
> > > }
>
> > See this:
>
> > > > createdocumenttype.php>
>
> I tried this approach. The xml content is now formatted the way I want
> to.
> The problem though is that $dom->validate() is returning false
> although the xml content is valid with respect to the DTD.
> Seems that the validate() function only works when loading xml files.
> Any thoughts?- Hide quoted text -
>
> - Show quoted text -

load your dtd in a browser with the xml file. basic but works fast

Re: Specifying a DTD for a DOMDocument

am 23.10.2007 17:28:21 von zeldorblat

On Oct 23, 9:34 am, bmic...@gmail.com wrote:
> On Oct 23, 3:13 pm, ZeldorBlat wrote:
>
>
>
> > On Oct 23, 5:22 am, bmic...@gmail.com wrote:
>
> > > Hey,
>
> > > I'd like to specify the DTD file for a DOMDocument on creation; so I
> > > can validate it.
> > > The DTD file already exists (let's call it content.dtd)
>
> > > $dom = new DOMDocument('1.0', 'utf-8');
> > > // Do something here to link cotnent.dtd to the DOMDocument. How do I
> > > do that?
>
> > > // Form the xml document.
> > > $dom->createElement('root_node');
> > > ...
>
> > > I found through the php documentation that I can validate xml content
> > > against a DTD if load the XML file (but not if I create it). Something
> > > like that (assuming the xml content is in file content.xml which
> > > specifies **):
>
> > > $dom = new DOMDocument;
> > > $dom->Load('content.xml');
> > > if ($dom->validate()) {
> > > // document is valid
>
> > > }
>
> > See this:
>
> > > > createdocumenttype.php>
>
> I tried this approach. The xml content is now formatted the way I want
> to.
> The problem though is that $dom->validate() is returning false
> although the xml content is valid with respect to the DTD.
> Seems that the validate() function only works when loading xml files.
> Any thoughts?

I use validate() with XML documents I've created without any
problems. Perhaps the DTD is inaccessible to the script?