Search code examples
perlxml-libxml

Is it possible for XML::LibXML to parse quirky tags?


Is it possible to get XML::LibXML to parse the node shown in the example below? I realize I may be creating invalid XML by specifying a '*' as part of the node name and would appreciate it if someone could explain why it is invalid:

use strict;
use warnings;
use XML::LibXML;

my $doc = XML::LibXML->createDocument;

my $quirky = XML::LibXML::Element->new( 'YAK*' );
$quirky->appendText( 'Important Data' );

$doc->setDocumentElement( $quirky );

print $doc->toString;  # <?xml version="1.0"?>
                       # <YAK*>Important Data</YAK*>

my $data = XML::LibXML
           ->new
             ->parse_string( $doc->toString );

Output:

<?xml version="1.0"?>
<YAK*>Important Data</YAK*>
:2: parser error : error parsing attribute name
<YAK*>Important Data</YAK*>
    ^
:2: parser error : attributes construct error
<YAK*>Important Data</YAK*>
    ^
:2: parser error : Couldn't find end of Start Tag YAK line 2
<YAK*>Important Data</YAK*>
    ^
:2: parser error : Extra content at the end of the document
<YAK*>Important Data</YAK*>
    ^

Solution

  • It will attempt work if you turn on a recover option–

    my $parser = XML::LibXML->new;
    $parser->recover_silently(1);
    my $doc2 = $parser->parse_string( $doc->toString );
    print $doc2->toString;
    

    But, as you see, though it can parse an invalid document it can't/won't roundtrip one–

    <?xml version="1.0"?>
    <YAK/>