DTDParser(3pm)

πŸ“¦
version 2.01 Β· 3 versions
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– DTDParser πŸ’–

okay bestie, so this is XML::DTDParser β€” a quick and dirty DTD parser. yes, the vibes are immaculate and also slightly unhinged. Version 2.01, no cap. ✨


🌸 SYNOPSIS 🌸

  use XML::DTDParser qw(ParseDTD ParseDTDFile);

  $DTD = ParseDTD $DTDtext;
 #or
 $DTD = ParseDTDFile( $dtdfile)

that's it. that's the whole entrance. she's lowkey simple like that. πŸ’…


πŸ¦‹ DESCRIPTION πŸ¦‹

so basically this module parses a DTD file and creates a data structure holding info about all the tags, their allowed parameters, children, parents, optionality etc. etc. etc. like, all of it, fr fr. πŸ‘‘

and because the original author was too lazy to document the structure (real), the move is: parse a DTD you actually need and print the result to a file using Data::Dumper. the data structure should be self-evident. it's giving "read the code, bestie." πŸ™ˆ

Note: the module should be able to parse just about anything, but it intentionaly loses some information. like, on purpose. it's not a bug, it's a lifestyle. πŸ’€

so for example β€” if the DTD says a tag should contain either CHILD1 or CHILD2, all you get back is that CHILD1 and CHILD2 are optional. meaning if the DTD contains <!ELEMENT FOO (BAR|BAZ)>, the result will be the same as if it contained <!ELEMENT FOO (BAR?,BAZ?)>. same energy, different spelling. 🀷

but wait, don't panic β€” you also get the original unparsed parameter list, so if you need that info you can absolutely parse it yourself. main character behavior honestly. πŸ’…


✨ the extension era (since version 1.6) ✨

since version 1.6 this module supports the author's "extensions" to DTDs. iconic. if the DTD contains a comment in this form:

        <!--#info element=XXX foo=bar greeting="Hello World!" person='d''Artagnan'-->

and there's an element XXX in the DTD, then the resulting hash for XXX will contain:

        'foo' => 'bar',
        'person' => 'd\'Artagnan',
        'greeting => 'Hello World!'

look at her go. 🌈

and if the DTD contains:

        <!--#info element=XXX attribute=YYY break=no-->

then:

        $DTD->{XXX}->{attributes}->{YYY}->[4]

will be set to:

        { break => 'no' }

yesss. chef's kiss. πŸŽ€


πŸ“ why tho? (the origin story) πŸ“

the author uses this parser to import the DTD into a database so they could map some fields to certain tags for output, and they wanted to be able to specify that mapping inside the file. so:

        <!--#info element=TagName map_to="FieldName"-->

and that's the whole pipeline. slay. πŸ’œ


πŸ’… EXPORT πŸ’…

by default the module exports all (both) of its functions. yes, both. just two. we love a small export list. if you only want one, or none, use:

        use XML::DTDParser qw(ParseDTD);
        or
        use XML::DTDParser qw();

🌟 ParseDTD 🌟

            $DTD = ParseDTD $DTDtext;

parses the $DTDtext and creates a data structure. if the $DTDtext contains some <!ENTITY ... SYSTEM "..."> declarations, those are read and parsed as needed. the paths are relative to the current directory.

the module currently doesn't support URLs here yet. just so you know. no links, bestie. 🚫

🌟 ParseDTDFile 🌟

            $DTD = ParseDTDFile $DTDfile;

parses the contents of $DTDfile and creates a data structure. if the $DTDfile contains some <!ENTITY ... SYSTEM "..."> declarations, those are read and parsed as needed. the paths are relative to $DTDfile this time. different base, same vibe. πŸ¦‹

the module currently doesn't support URLs here yet either. still no links. we stay offline. πŸ™ˆ

🌟 FindDTDRoot 🌟

            $DTD = ParseDTD $DTDtext;
            @roots = FindDTDRoot $DTD;

returns all tags that have no parent. there could be several such tags defined by the DTD β€” especially if it used some common includes. multiple roots? more like multiple icons. periodt. πŸ‘‘


🫢 AUTHOR 🫢

Jenda@Krynicky.cz β€” http://Jenda.Krynicky.cz


Copyright (c) 2002 Jan Krynicky <Jenda@Krynicky.cz>. All rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


okay that's the whole page, bestie, you ate. this doc came from perl v5.36.0 and was dated 2022-11-21, and that's the tea. go parse something cute. πŸ’–βœ¨πŸŽ€πŸ¦‹πŸŒΈ

πŸ“¦ where this page comes from