The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

XML::Filter::NSNormalise - SAX filter to normalise namespace prefixes

SYNOPSIS

use XML::SAX::Machines qw( :all );
use XML::Filter::NSNormalise;

my $p = Pipeline(
  XML::Filter::NSNormalise->new(
    Map => {
      'http://purl.org/dc/elements/1.1/' => 'dc',
      'http://purl.org/rss/1.0/modules/syndication/' => 'syn'
    }
  )
  => \*STDOUT
);

$p->parse_uri($filename);

DESCRIPTION

This SAX (version 2) filter can be used to transform documents to ensure the prefixes associated with namespaces are used consistently.

For example, feeding this document...

<rdf:RDF
 xmlns="http://purl.org/rss/1.0/"
 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
 xmlns:theonetruedublincore="http://purl.org/dc/elements/1.1/" >
  <theonetruedublincore:date>2002-10-08</theonetruedublincore:date>
</rdf:RDF>

... through this filter ...

XML::Filter::NSNormalise->new(
  Map => {
    'http://purl.org/dc/elements/1.1/' => 'dc'
  }
)

... would produce this output ...

<rdf:RDF
 xmlns="http://purl.org/rss/1.0/"
 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
 xmlns:dc="http://purl.org/dc/elements/1.1/" >
  <dc:date>2002-10-08</dc:date>
</rdf:RDF>

You can specify more than one namespace URI to prefix mapping, eg:

XML::Filter::NSNormalise->new(
  Map => {
    'http://purl.org/dc/elements/1.1/' => 'dc',
    'http://www.w3.org/1999/02/22-rdf-syntax-ns#' => 'rdf',
    'http://purl.org/rss/1.0/modules/syndication/' => 'syn'
  }
)

METHODS

new()

The constructor expects a list of options as Key => Value pairs.

The 'Map' option must be specified and must be set to a hashref. Each key of the hashref is a namespace URI and each value is the corresponding namespace prefix you want in the output document. Any namespaces which occur in the document but do not occur in the Map hash, will be passed through unaltered.

All other options are passed to the default constructor in XML::SAX::Base.

ERROR HANDLING

Attempting to map more than one URI to the same prefix will cause a fatal exception, eg:

XML::Filter::NSNormalise->new(
  Map => {
    'http://x.com/ => 'z',
    'http://y.com/ => 'z'
  }
)

Attempting to map a URI to a prefix that is already mapped to a different URI will cause a fatal exception (eg: you map a URI to the prefix 'foo' but the document your are filtering already uses 'foo' for a different URI).

SEE ALSO

XML::SAX, XML::SAX::Base, XML::SAX::Machines.

COPYRIGHT

Copyright 2002 Grant McLean <grantm@cpan.org>

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