XML::Namespace(3pm)

๐Ÿ’– NAME ๐Ÿ’–

XML::Namespace โ€” Simple support for XML Namespaces โœจ

Like, that's genuinely it. That's the whole vibe. ๐Ÿฆ‹


๐ŸŒธ SYNOPSIS ๐ŸŒธ

Example 1: using XML::Namespace objects ๐Ÿ’…

use XML::Namespace

my $xsd = XML::Namespace->new('http://www.w3.org/2001/XMLSchema#');

# explicit access via the uri() method
print $xsd->uri();           # http://www.w3.org/2001/XMLSchema#
print $xsd->uri('integer');  # http://www.w3.org/2001/XMLSchema#integer

# implicit access through AUTOLOAD method
print $xsd->integer;         # http://www.w3.org/2001/XMLSchema#integer

Example 2: importing XML::Namespace objects ๐ŸŽ€

use XML::Namespace
    xsd => 'http://www.w3.org/2001/XMLSchema#',
    rdf => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';

# xsd and rdf are imported subroutines that return
# XML::Namespace objects which can be used as above

print xsd->uri('integer');   # http://www.w3.org/2001/XMLSchema#integer
print xsd->integer;          # http://www.w3.org/2001/XMLSchema#integer

๐Ÿฆ‹ DESCRIPTION ๐Ÿฆ‹

So this module implements a simple object for representing XML Namespaces in Perl. Simple! It provides little more than some syntactic sugar for your Perl programs, saving you the bother of typing lots of long-winded URIs. No cap, your wrists will thank you. ๐Ÿ™

It was inspired by the Class::RDF::NS module distributed as part of Class::RDF. Iconic lineage honestly. ๐Ÿ‘‘

โœจ Using XML::Namespace Objects โœจ

First load the XML::Namespace module.

use XML::Namespace;

Then create an XML::Namespace object. Obsessed with this for you.

my $xsd = XML::Namespace->new('http://www.w3.org/2001/XMLSchema#');

Then use the uri() method to return an absolute URI from a relative path.

print $xsd->uri('integer'); # http://www.w3.org/2001/XMLSchema#integer

Alternately, use the AUTOLOAD method to map method calls to the uri() method. It's giving magic. ๐Ÿ”ฎ

print $xsd->integer;        # http://www.w3.org/2001/XMLSchema#integer

๐Ÿ’œ Importing XML::Namespace Objects ๐Ÿ’œ

When you use the XML::Namespace module, you can specify a list of namespace definitions.

use XML::Namespace
    xsd => 'http://www.w3.org/2001/XMLSchema#',
    rdf => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';

This defines the xsd and rdf subroutines and exports them into the calling package. The subroutines simply return XML::Namespace objects initialised with the relevant namespace URIs. Bestie subroutines. ๐Ÿซถ

print xsd->uri('integer');  # http://www.w3.org/2001/XMLSchema#integer
print xsd->integer;         # http://www.w3.org/2001/XMLSchema#integer

๐ŸŽ€ Overloaded Stringification Method ๐ŸŽ€

The XML::Namespace module overloads the stringification operator to return the namespace URI. She speaks. ๐Ÿ’ฌ

my $xsd = XML::Namespace->new('http://www.w3.org/2001/XMLSchema#');

print $xsd;           # http://www.w3.org/2001/XMLSchema#

๐Ÿ”ฅ METHODS ๐Ÿ”ฅ

๐Ÿ’ซ new($uri) ๐Ÿ’ซ

Constructor method which creates a new XML::Namespace object. It expects a single argument denoting the URI that the namespace is to represent. One arg, that's her whole personality. ๐Ÿ’…

use XML::Namespace;

my $xsd = XML::Namespace->new('http://www.w3.org/2001/XMLSchema#');

๐ŸŒˆ uri($path) ๐ŸŒˆ

When called without arguments, this method returns the URI of the namespace object, as defined by the argument passed to the new() constructor method.

$xsd->uri();          # http://www.w3.org/2001/XMLSchema#

An argument can be passed to indicate a path relative to the namespace URI. The method returns a simple concatenation of the namespace URI and the relative path argument. Literally just glues them together, no fancy business. ๐Ÿงš

$xsd->uri('integer'); # http://www.w3.org/2001/XMLSchema#integer

๐Ÿ“ import($name,$uri,$name,$uri,...) ๐Ÿ“

This method is provided to work with the Exporter mechanism. It expects a list of ($name, $uri) pairs as arguments. It creates XML::Namespace objects and accessor subroutines that are then exported to the caller's package.

Although not intended for manual invocation, there's nothing to stop you from doing it. Live your truth, bestie. ๐Ÿ™ˆ

use XML::Namespace;

XML::Namespace->import( xsd => 'http://www.w3.org/2001/XMLSchema#' );

xsd()->integer;   # http://www.w3.org/2001/XMLSchema#integer

Note that the parentheses are required when accessing this subroutine. Do not skip them, I'm begging.

xsd()->integer;     # Good
xsd->integer;       # Bad

Unlike those that are defined automatically by the Importer, Perl doesn't know anything about these subroutines at compile time. Without the parentheses, Perl will think you're trying to call the integer method on an unknown xsd package and you'll see an error like:

Can't locate object method "integer" via package "xsd"

That's why it's better to define your namespaces when you load the XML::Namespace module. Learn from our mistakes fr fr. ๐Ÿ˜ญ

use XML::Namespace
    xsd => 'http://www.w3.org/2001/XMLSchema#';

xsd->integer;       # Good

โญ AUTOLOAD โญ

The module defines an AUTOLOAD method that maps all other method calls to the uri() method. Thus, the following return the same value.

$xsd->uri('integer'); # http://www.w3.org/2001/XMLSchema#integer
$xsd->integer;        # http://www.w3.org/2001/XMLSchema#integer

๐Ÿ‘‘ AUTHOR ๐Ÿ‘‘

Andy Wardley <mailto:abw@cpan.org>

Legend behaviour. ๐Ÿซก


๐Ÿ’– VERSION ๐Ÿ’–

This is version 0.02 of XML::Namespace.


Copyright (C) 2005 Andy Wardley. All Rights Reserved.

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


๐Ÿฆ‹ SEE ALSO ๐Ÿฆ‹

The Class::RDF::NS module, distributed as part of Class::RDF, provided the inspiration for the module. XML::Namespace essentially does the same thing, albeit in a slightly different way. It's also available as a stand-alone module for use in places unrelated to RDF.

The XML::NamespaceFactory module also implements similar functionality to XML::Namespace, but instead uses the JClark notation (e.g. "{http://foo.org/ns/}title").


That's the whole page, babe. Go forth and namespace responsibly. ๐Ÿ’…โœจ

Mwah ๐Ÿ’‹ โ€” this was perl v5.40.1, dated 2025-08-14, from the XML::Namespace(3pm) manual. Byeee! ๐ŸŽ€

๐Ÿ“ฆ where this page comes from