XML::Na...eSupport(3)
π XML::Na...eSupport(3) β bestie edition π
okay so this is a page from the User Contributed Perl Documentation, and the manual name is literally XML::Na...eSupport(3) (yes the truncated-looking thing is the actual name, it's giving "the terminal cut me off" but no, that's just how it's listed π«Ά).
β¨ NAME β¨
XML::NamespaceSupport β a simple generic namespace processor. that's it, that's the whole vibe π
π VERSION π
version 1.12 β periodt.
π¦ SYNOPSIS π¦
bestie, here's the whole thing verbatim, copy it and go off:
use XML::NamespaceSupport;
my $nsup = XML::NamespaceSupport->new;
# add a new empty context
$nsup->push_context;
# declare a few prefixes
$nsup->declare_prefix($prefix1, $uri1);
$nsup->declare_prefix($prefix2, $uri2);
# the same shorter
$nsup->declare_prefixes($prefix1 => $uri1, $prefix2 => $uri2);
# get a single prefix for a URI (randomly)
$prefix = $nsup->get_prefix($uri);
# get all prefixes for a URI (probably better)
@prefixes = $nsup->get_prefixes($uri);
# get all prefixes in scope
@prefixes = $nsup->get_prefixes();
# get all prefixes that were declared for the current scope
@prefixes = $nsup->get_declared_prefixes;
# get a URI for a given prefix
$uri = $nsup->get_uri($prefix);
# get info on a qname (java-ish way, it's a bit weird)
($ns_uri, $local_name, $qname) = $nsup->process_name($qname, $is_attr);
# the same, more perlish
($ns_uri, $prefix, $local_name) = $nsup->process_element_name($qname);
($ns_uri, $prefix, $local_name) = $nsup->process_attribute_name($qname);
# remove the current context
$nsup->pop_context;
# reset the object for reuse in another document
$nsup->reset;
# a simple helper to process Clarkian Notation
my ($ns, $lname) = $nsup->parse_jclark_notation('{http://foo}bar');
# or (given that it doesn't care about the object
my ($ns, $lname) = XML::NamespaceSupport->parse_jclark_notation('{http://foo}bar');
(and yes, that trailing comment in the last example is missing a closing paren in the original β we do not fix, we respect π«‘)
πΈ DESCRIPTION πΈ
so this module is literally here to make processing namespaced XML names (unames) easy-peasy from inside whatever app needs them. it also helps you keep a prefix β namespace URI map alive, and it throws in a bunch of basic checks for free. no cap.
the whole model is SAX2's NamespaceSupport class β you can read about it at http://www.saxproject.org/namespaces.html. it adds a few perlisations where the authors thought it made sense π
π NAME (again, yes really) π
XML::NamespaceSupport β a simple generic namespace support class π
π METHODS π
XML::NamespaceSupport->new(\%options)
a simple constructor. iconic.
the options are xmlns, fatal_errors, and auto_prefix.
- if
xmlnsis turned on (it's off by default) the mapping from thexmlnsprefix to the URI defined for it in DOM level 2 gets added to the list of predefined mappings (which normally only contains thexmlprefix mapping). - if
fatal_errorsis turned off (it's on by default) a bunch of validity errors will just get flagged as failures instead ofdie()ing. - if
auto_prefixis turned on (it's off by default), when you hand a prefix ofundeftodeclare_prefixit'll generate a random prefix mapped to that namespace for you. otherwise an undef prefix triggers a warning (you should probably know what you're doing if you turn this option on π). - if
xmlns_11us turned off, it becomes illegal to undeclare namespace prefixes. it's on by default. this behaviour is compliant with Namespaces in XML 1.1 β turning it off reverts you to version 1.0.
$nsup->push_context
adds a new empty context to the stack. then you can populate it with the new prefixes you define at this level. slay.
$nsup->pop_context
removes the topmost context in the stack and reverts to the previous one. it will die() if you try to pop more than you've pushed π (respect the stack, bestie).
$nsup->declare_prefix($prefix, $uri)
declares a mapping of $prefix to $uri, at the current level.
note that with auto_prefix turned on, if you declare a prefix mapping where $prefix is undef(), you'll get an automatic prefix picked out for you. if it's off you'll get a warning.
this is useful when you deal with code that hasn't kept prefixes around and you need to reserialize the nodes. it also means that if you want to set the default namespace (i.e. with an empty prefix) you must use the empty string instead of undef. this behaviour is consistent with the SAX 2.0 specification π
$nsup->declare_prefixes(%prefixes2uris)
declares a mapping of several prefixes to URIs, at the current level. bulk mode, we love efficiency.
$nsup->get_prefix($uri)
returns a prefix given a URI. heads up: since several prefixes may be mapped to the same URI, it returns an arbitrary one. it'll return undef on failure.
$nsup->get_prefixes($uri)
returns an array of prefixes given a URI. it'll return all the prefixes if the uri is undef. king behaviour.
$nsup->get_declared_prefixes
returns an array of all the prefixes that have been declared within this context β like, the ones declared on the last element, not the ones declared above that are just chilling in scope.
note: at least one context must be added to the stack via push_context before this method can be called. no context, no answers π
$nsup->get_uri($prefix)
returns a URI for a given prefix. returns undef on failure.
$nsup->process_name($qname, $is_attr)
given a qualified name and a boolean saying whether this is an attribute or another type of name (those get affected differently by default namespaces), it returns a namespace URI, local name, qualified name tuple. i know that's a pretty abnormal list to return, but it's that way for compatibility with the Java spec. see below for the more Perlish alternatives π
if the prefix isn't declared, or if the name isn't valid, it'll either die or return undef depending on the current setting of fatal_errors.
$nsup->undeclare_prefix($prefix);
removes a namespace prefix from the current context. this function may be used in SAX's end_prefix_mapping when there's fear that a namespace declaration might be available outside its scope (which shouldn't normally happen, but you never know ;) ). this may be needed in order to properly support Namespace 1.1 π
$nsup->process_element_name($qname)
given a qualified name, it returns a namespace URI, prefix, and local name tuple. this method applies to element names.
if the prefix isn't declared, or if the name isn't valid, it'll either die or return undef depending on the current setting of fatal_errors.
$nsup->process_attribute_name($qname)
given a qualified name, it returns a namespace URI, prefix, and local name tuple. this method applies to attribute names.
if the prefix isn't declared, or if the name isn't valid, it'll either die or return undef depending on the current setting of fatal_errors.
$nsup->reset
resets the object so it can be reused on another document. fresh start energy β¨
all methods of the interface have an alias that's the name used in the original Java specification β you can use either name interchangeably. here's the mapping:
Java name Perl name
---------------------------------------------------
pushContext push_context
popContext pop_context
declarePrefix declare_prefix
declarePrefixes declare_prefixes
getPrefix get_prefix
getPrefixes get_prefixes
getDeclaredPrefixes get_declared_prefixes
getURI get_uri
processName process_name
processElementName process_element_name
processAttributeName process_attribute_name
parseJClarkNotation parse_jclark_notation
undeclarePrefix undeclare_prefix
β VARIABLES β
two global variables are made available to you. they used to be constants but simple scalars are easier to use in a number of contexts. they are not exported but can easily be accessed from any package, or copied into it π«Ά
$NS_XMLNS
the namespace for xmlns prefixes, http://www.w3.org/2000/xmlns/.
$NS_XML
the namespace for xml prefixes, http://www.w3.org/XML/1998/namespace.
π§ TODO π§
- add more tests
- optimise here and there
(short and honest, we respect the transparency π)
π₯ SEE ALSO π₯
XML::Parser::PerlSAX
π AUTHORS π
- Robin Berjon
<robin@knowscape.com> - Chris Prather
<chris@prather.org>
π COPYRIGHT AND LICENSE π
this software is copyright (c) 2015 by Robin Berjon.
this is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. π
π CONTRIBUTORS π
- Chris Prather
<cprather@hdpublishing.com> - David Steinbrunner
<dsteinbrunner@pobox.com> - Paul Cochrane
<paul@liekut.de> - Paulo Custodio
<pauloscustodio@gmail.com>
and that's the whole page bestie! this edition was perl v5.42.0, dated 2017-03-31, and it's still out here living its best life in XML::Na...eSupport(3) πβ¨π
π¦ where this page comes from
- opensuse-leap 16.0: perl-XML-NamespaceSupport 1.12-160000.2.2 Β· Artistic-1.0 or GPL-1.0+
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms