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


XML::Easy::Transform::RationalizeNamespacePrefixes - rationalize namespaces prefixes


  use XML::Easy::Transform::RationalizeNamespacePrefixes qw(

  my $doc = rationalize_namespace_prefixes(


This code creates a new tree of XML::Easy::Element nodes by examining an existing XML::Easy::Element tree and producing a new tree that is schemantically identical under the XML Namespaces 1.0 specification but with all namespace declartions moved to the top node of the tree (this may involve renaming several elements in the tree to have different prefixes.)

It supplies one public function that can do this transformation which is exported on request:

rationalize_namespace_prefixes($easy_element, $generator_subref)
rationalize_namespace_prefixes($easy_element, $options_hashref)

The first argument is a XML::Easy::Element that you wish a transformed copy of to be returned. An exception will be thrown if thrown if the XML document is not namespace-well-formed (i.e. it breaches the XML Namespaces 1.0 specification.)

The second (optional) argument may be a reference to a function that should, when passed a string containing a xml prefix as its first argument, return a string containing an alternative xml prefix. If no function is passed in then the default renaming function is used, which will append or replace trailing numbers with higher numbers to the prefix.

Alternativly, a hashref may be passed as the (optional) second arguement. The keys of this hash may be:


The prefix generating subroutine reference, as previously described.


A hashref containing a mapping of namespace to prefixes that you want to force to be declared. This enables you to control exactly what prefixes are used for what namespaces and to force additional namespace declarations for namespaces not otherwise mentioned in the XML document you are transforming. Specifying more than one namespace that maps to the same prefix will cause an exception to be thrown.


By default attributes without a prefix have the same namespace as the element that they belong to. Setting this to a true value will force prefixes to be prepended to attribute names even if they could be ommited.

The new XML::Easy::Element will be returned as the only return value of this function.


A Basic Transform

After defining a handy utility function:

  sub process($) {
    return xml10_write_document(
        xml10_read_document( $_[0] )

This code:

  print process <<'XML';
    <ex1:bar xmlns:ex1=""/>

Moves the namespace up and prints:

  <foo xmlns:ex1="">

Creating Prefixes

If you use the same prefix twice in the document to refer to different namespaces then the function will rename one of the prefixes:

  print process <<'XML';
  <muppet:kermit xmlns:muppet="">
    <muppet:kermit xmlns:muppet="" />


  <muppet:kermit xmlns:muppet="" xmlns:muppet2="">
    <muppet2:kermit />

This works for the default namespace too:

  print process <<'XML';
    <bar xmlns="" />


  <foo xmlns:default2="">
    <default2:bar />

If you want control on how your prefixes will be renamed you can supply a function as the second arguement to rationalize_namespace_prefixes.

  my $transformed = rationalize_namespace_prefixes(
    sub {
      my $name = shift;
      $name =~ s/\d+\Z//;
      return $name . int(rand(10000));

If your function returns a prefix that has already been used it will be called again and again until it returns an unused prefix. The first time the function is called it will be passed the prefix from the source, and if it is called subsequent times after that because the new prefix it previously returned is already in use it will be passed the prefix the previous call to the function created.

Removing Unneeded Prefixes

This module also removes all unnecessary prefixes:

  <wobble xmlns:ex1="">
    <ex1:wibble ex1:jelly="in my tummy" />
    <ex2:bobble xmlns:ex2="" />

Will be transformed into

  <wobble xmlns:ex1="">
    <ex1:wibble jelly="in my tummy" />
    <ex1:bobble />


Written by Mark Fowler <>

Copyright Photobox 2009. All Rights Reserved.

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


None known.

Please report bugs via RT

The version control system for this module is hosted on github. Please feel free to fork and send pull requests.