man XML::Doctype () - A DTD object class


XML::Doctype - A DTD object class


   # To parse an external DTD at compile time, useful when
   # using XML::ValidWriter
   use XML::Doctype NAME => 'FooML', SYSTEM_ID => 'FooML.dtd' ;
   use XML::Doctype NAME => 'FooML', DTD_TEXT  => $dtd ;

   # Parsing at run-time
   $doctype = XML::Doctype->new( 'FooML', SYSTEM_ID => 'FooML.dtd' ) ;

   # or
   $doctype = XML::Doctype->new() ;
   $doctype->parse( 'FooML', 'FooML.dtd' ) ;

   # Saving the parsed object
   open( PM, ">FooML/DTD/" ) or die $! ;
   print PM $doctype->as_pm( 'FooML::DTD::v1_000' ) ;

   # Using a saved parsed DTD
   use FooML::DTD::v1_000 ;

   $doctype = FooML::DTD::v1_000->new() ;


This module parses DTDs and allows them to be saved as .pm files and reloaded. The ability to save and reload is intended to aid in packaging parsed DTDs with XML tools so that XML::Parser need not be installed.


This module is alpha code. It's developed enough to support XML::ValidWriter, but need a lot of work. Some big things that are lacking are:

methods or objects to build / traverse the DTD


   $doctype = XML::Doctype->new() ;
   $doctype = XML::Doctype->new( 'FooML', DTD_TEXT => $doctype_text ) ;
   $doctype = XML::Doctype->new( 'FooML', SYSTEM_ID => 'FooML.dtd' ) ;
   $name = $doctype->name() ;

   Sets/gets the name.
   $doctype->parse_dtd( $name, $doctype_text ) ;
   $doctype->parse_dtd( $name, $doctype_text, 'internal' ) ;

Parses the text of a DTD from a scalar. CW$name is used to indicate the name of the DOCTYPE, and thus the root node.

The DTD is considered to be external unless the third parameter is TRUE.

   $doctype->parse_dtd_file( $name, $system_id [, $public_id] ) ;
   $doctype->parse_dtd_file( $name, $system_id [, $public_id], 'internal' ) ;

Parses a DTD from a file. Eventually will support full URL syntax.

$public_id is ignored for now, and CW$system_id is used to locate the DTD.

This routine requires XML::Parser. XML::Parser is not loaded at any other time and is not needed to use the resulting DTD object.

The DTD is considered to be external unless the fourth parameter is TRUE.

   $doctype->parse_dtd_file( $name, $system_id, $p_id, 'internal' ) ;
   $doctype->parse_dtd_file( $name, $system_id, undef, 'internal' ) ;
   $system_id = $doctype->system_id() ;

   Sets/gets the system ID.
   $public_id = $doctype->public_id() ;

   Sets/gets the public_id.
   $elt_decl = $doctype->element_decl( $name ) ;

Returns the XML::Doctype:Element object associated with CW$name. These can be defined by <!ELEMENT> tags or undefined, which can happen if they were just referred-to by <!ELEMENT> or <!ATTLIST> tags.

Returns an unsorted list of element names. This list includes names that are declared and undeclared (but referred to in element declarations or attribute definitions).
   open( PM, "FooML/DTD/" )            or die $! ;
   print PM $doctype->as_pm( 'FooML::DTD::v1_001' ) or die $! ;
   close PM                                     or die $! ;

Then, later:

   use FooML::DTD::v1_001 ;   # Do *not* use () as a parameter list!

Returns string containing the DTD as an independant module, allowing the DTD to be parsed in the development environment and shipped as Perl code, so that the target environment need not have XML::Parser installed.

This is useful for XML creation-only tools and as an efficiency tuning measure if you will be rereading the same set of DTDs over and over again.

   use XML::Doctype NAME => 'FooML', SYSTEM_ID => 'dtds/FooML.dtd' ;

import() constructs a default DTD object for the calling package so that XML::ValidWriter's functional interface can use it.

If XML::Doctype is subclassed, the subclasses' constructor is called with all parameters.


This object uses the fields pragma, so you should use base and fields for any subclasses.


Barrie Slaymaker <>


This module is Copyright 2000, Barrie Slaymaker. All rights reserved.

This module is licensed under the GPL, version 2. Please contact me if this does not suit your needs.