XML::Doctype(3pm)
π NAME π
XML::Doctype β bestie, it's a DTD object class β¨
π¦ SYNOPSIS π¦
# 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/v1_000.pm" ) 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() ;
πΈ DESCRIPTION πΈ
Okay so this module parses DTDs and lets you save them out as .pm files and reload them later. And the whole save-and-reload thing? It's not just for fun β the point is so you can package parsed DTDs alongside your XML tools and then XML::Parser doesn't even have to be installed on the machine that runs them. Iconic behaviour, honestly. π
β STATUS β
Not gonna lie bestie, this module is alpha code. It's cooked enough to hold up XML::ValidWriter, but it still needs a lot of work. Some of the big things that are missing:
- methods or objects to build / traverse the DTD π
XML::Doctype::ELEMENTXML::Doctype::ATTLISTXML::Doctype::ENITITY
Yes, ENITITY is spelled exactly like that in here. Not me fixing it. We keep it fr fr. π
β¨ METHODS β¨
π new
$doctype = XML::Doctype->new() ;
$doctype = XML::Doctype->new( 'FooML', DTD_TEXT => $doctype_text ) ;
$doctype = XML::Doctype->new( 'FooML', SYSTEM_ID => 'FooML.dtd' ) ;
The constructor. Main character energy, obviously. π
π name
$name = $doctype->name() ;
Sets/gets the name. That's it, that's the whole vibe. β¨
π§ parse_dtd
$doctype->parse_dtd( $name, $doctype_text ) ;
$doctype->parse_dtd( $name, $doctype_text, 'internal' ) ;
Parses the text of a DTD straight from a scalar. $name tells you the name of the DOCTYPE, which means it's also the root node.
The DTD is considered external unless the third parameter is TRUE. Periodt.
π«Ά parse_dtd_file
$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 it'll support full URL syntax β for now that's a someday thing, lowkey.
$public_id is ignored for now, and $system_id is what actually gets used to locate the DTD.
Heads up bestie: this routine requires XML::Parser. It is not loaded at any other time, and you do not need it to use the resulting DTD object. So it's a one-and-done kind of dependency. π¦
The DTD is considered 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
$system_id = $doctype->system_id() ;
Sets/gets the system ID. π
πΈ public_id
$public_id = $doctype->public_id() ;
Sets/gets the public_id.
π element_decl
$elt_decl = $doctype->element_decl( $name ) ;
Returns the XML::Doctype:Element object that goes with $name. These can be defined by <!ELEMENT> tags β or undefined, which happens when they were only just referred-to by <!ELEMENT> or <!ATTLIST> tags. Rude, but it happens. π
π element_names
Returns an unsorted list of element names. And this list includes both the declared names and the undeclared ones (the ones that got name-dropped in element declarations or attribute definitions). Everybody's invited to this list. π
π
as_pm
open( PM, "FooML/DTD/v1_001.pm" ) 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 a string containing the DTD as an independent module, which means you can parse the DTD in your dev environment and ship it as plain Perl code β so the target environment never needs XML::Parser installed. Chef's kiss. π
This is genuinely useful for XML creation-only tools, and it's also an efficiency tuning measure if you're gonna be rereading the same set of DTDs over and over and over again. π₯
π¦ import
π¦ use
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. Teamwork makes the dream work. π
If XML::Doctype gets subclassed, the subclasses' constructor is called with all the parameters. No cap.
π§ SUBCLASSING π§
This object uses the fields pragma, so bestie, for any subclasses you should use base and fields. That's the whole recipe. π
π AUTHOR π
Barrie Slaymaker <barries@slaysys.com>
π COPYRIGHT π
This module is Copyright 2000, 2005 Barrie Slaymaker. All rights reserved.
This module is licensed under your choice of the Artistic, BSD or General Public License. Pick your fighter. π
okay that's the whole page bestie, ily, go parse some DTDs β¨
This was perl v5.34.0, dated 2022-06-28 β mwah, bye! π«Άππ¦
π¦ where this page comes from
- debian bookworm: libxml-autowriter-perl 0.40-5 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libxml-autowriter-perl 0.40-5 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libxml-autowriter-perl 0.40-5 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libxml-autowriter-perl 0.40-5 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libxml-autowriter-perl 0.40-5 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms