XML technologies / XML / Perl & XML / SAX / DTD handlers

(Excerpt from "Perl & XML" by Erik T. Ray & Jason McIntosh)

XML::Parser::PerlSAX supports another group of handlers for DTD events. They handle everything before the root element, including the XML declaration, the document type declaration, internal subset, and so on, collectively called the document prolog. To reproduce a document completely and faithfully, as in a filter, implement some of these handlers to preserve the prolog. This was missing from the previous example.

These handlers have other uses, such as preloading entity definitions for special handling, though normally the parser does this. The following table lists the DTD handlers.

Table: PerlSAX DTD handlers

Methodenname

Event

Properties

entity_decl

Der Parser hat eine Entitydeklaration gelesen. Dieses Event ist stets dasselbe, egal ob internes oder externes Entity oder vom Parser analysiert. Vom Parser nicht analysierte Entities werden eventuellnormalerweise auch über dieses Event gemeldet.

Name, Value, PublicId, SystemId, Notation

notation_decl

Der Parser meldet die Deklaration einer Notation.

Name, PublicId, SystemId, Base

unparsed_entity_decl

Der Parser hat die Deklaration eines nichtanalysierten Entity gefunden (zum Beispiel ein Entity mit Binärdaten). Fehlt dieser Handler, wird das Event mit Hilfe von entity_decl() gemeldet.

Name, PublicId, SystemId, Base

element_decl

Eine Elementdeklaration wurde gefunden.

Name, Model

attlist_decl

Eine Attributdeklaration wurde gefunden. Jedes Attribut wird einzeln gemeldet, auch wenn mehrere in einer Attributliste deklariert werden.

ElementName, AttributeName, Type, Fixed

doctype_decl

Die Dokumenttypdeklaration wird vom Parser gemeldet.

Name, SystemId, PublicId, Internal

xml_decl

Eine XML-Deklaration wurde gefunden.

Version, Encoding, Standalone

The handler entity_decl( ) handles all entity declarations unless a more specific handler exists. An unparsed entity declaration calls the handler entity_decl( ), unless the handler unparsed_entity_decl( ) is defined and takes precedence.

Parameters of an entity_decl( ) handler depend on entity type. For internal entities the Value property is set; otherwise it is absent. PublicId and SystemId locate an external entity’s content file, but do not apply to internal entities. The base (Base) is a URL resolving a relative SystemId.

Notation declarations assign a special type identifier to an entity, for example “date”, indicating that it contains a date. Notations are rarely used in practice, so we can safely omit them here.

The property Model of the handler element_decl( ) contains the element grammar, called its content model, describing what the DTD permits inside it.

An attribute-list declaration may declare several attributes. The parser handles this, passing the handler attlist_decl( ) one attribute at a time.

The optional document type declaration follows the XML declaration. Its parameter Name is the root element’s name. PublicId and SystemId tell the processor where to load an external DTD. The parameter Internal contains the entire internal subset as a string if individual entity and element declarations are to be skipped.

To extend our filter to pass the prolog through unchanged, use the following handlers.

Example: An improved filter

# Behandlung von XML-Deklarationen     
#
sub xml_decl {
     my( $self, $properties ) = @_;
     output( "<?xml version=\"" . $properties->{'Version'} . "\"" );
     my $encoding = $properties->{'Encoding'};
     output( " encoding=\"$encoding\"" ) if( $encoding );
     my $standalone = $properties->{'Standalone'};
     output( " standalone=\"$standalone\"" ) if( $standalone );
     output( "?>\n" );
}

#
# Behandlung von Dokumenttypdeklarationen:
# Duplikation des Originals, so gut wie möglich
#
sub doctype_decl {
     my( $self, $properties ) = @_;
     output( "\n<!DOCTYPE " . $properties->{'Name'} . "\n" );
     my $pubid = $properties->{'PublicId'};
     if( $pubid ) {
     output( " PUBLIC \"$pubid\"\n" );
     output( " \"" . $properties->{'SystemId'} . "\"\n" );
     } else {
     output( " SYSTEM \"" . $properties->{'SystemId'} . "\"\n" );
     }
     my $intset = $properties->{'Internal'};
     if( $intset ) {
     $in_intset = 1;
     output( "[\n" );
     } else {
    output( ">\n" );
  }
}

#
# Behandlung von Entitydeklarationen im internen Bereich:
# Stelle die ursprüngliche Deklaration wieder her
#
sub entity_decl {
     my( $self, $properties ) = @_;
     my $name = $properties->{'Name'};
     output( "<!ENTITY $name " );
     my $pubid = $properties->{'PublicId'};
     my $sysid = $properties->{'SystemId'};
     if( $pubid ) {
     output( "PUBLIC \"$pubid\" \"$sysid\"" );
     } elsif( $sysid ) {
     output( "SYSTEM \"$sysid\"" );
     } else {
     output( "\"" . $properties->{'Value'} . "\"" );
     }
     output( ">\n" );
}

The next example shows the improved filter’s output.

Example: Improved filter output

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE book
SYSTEM "/usr/local/prod/sgml/db.dtd"
[
<!ENTITY schwachsinn "hoo hah blah blah">
]>
<book id="mybook">
    <title>Einführung in GRXL</title>
    <chapter id="intro">
        <title>Was ist GRXL?</title>
        <comment> Der Titel sollte schmissiger werden. </comment>
        <para>
            Einfach ein weiteres Akronym. Das war unsere ursprüngliche Absicht, aber dann stellte
            sich mehr und mehr heraus, wie verblüffend nützlich diese neue Technologie namens
            <literal>GRXL</literal> ist. Betrachten Sie das folgende Programm:
        </para>
        <programlisting>AH aof -- %%%%
{{{{{{ let x = 0 }}}}}}
print! <lineannotation>wow</lineannotation>
oder nicht!</programlisting>
        <comment> Welchen Font sollten wir hier benutzen? </comment>
        <para>
            Was das Programm macht? Wen kümmert’s? Es sieht einfach cool aus, oder nicht?
            Wirklich, ich kann echt nur eines sagen: "&schwachsinn;".
        </para>
    </chapter>
</book>

Much better: a complete filter. The main handler processes elements and content; DTD handlers process what lies outside the root.

  

  

 

 

 

back to top