XML::NamespaceSupport - a simple generic namespace support class
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');
The model for this module is SAX2's NamespaceSupport class, readable at http://www.megginson.com/SAX/Java/javadoc/org/xml/sax/helpers/NamespaceSupport.html. It adds a few perlisations where we thought it appropriate.
The options are "xmlns", "fatal_errors", and "auto_prefix"
If "xmlns" is turned on (it is off by default) the mapping from the xmlns prefix to the URI defined for it in DOM level 2 is added to the list of predefined mappings (which normally only contains the xml prefix mapping).
If "fatal_errors" is turned off (it is on by default) a number of validity errors will simply be flagged as failures, instead of die()ing.
If "auto_prefix" is turned on (it is off by default) when one provides a prefix of "undef" to "declare_prefix" it will generate a random prefix mapped to that namespace. Otherwise an undef prefix will trigger a warning (you should probably know what you're doing if you turn this option on).
If "xmlns_11" us turned off, it becomes illegal to undeclare namespace prefixes. It is on by default. This behaviour is compliant with Namespaces in XML 1.1, turning it off reverts you to version 1.0.
Note that with "auto_prefix" turned on, if you declare a prefix mapping in which $prefix is undef(), you will get an automatic prefix selected for you. If it is off you will get a warning.
This is useful when you deal with code that hasn't kept prefixes around and need to reserialize the nodes. It also means that if you want to set the default namespace (ie with an empty prefix) you must use the empty string instead of undef. This behaviour is consistent with the SAX 2.0 specification.
If the prefix is not declared, or if the name is not valid, it'll either die or return undef depending on the current setting of "fatal_errors".
If the prefix is not declared, or if the name is not valid, it'll either die or return undef depending on the current setting of "fatal_errors".
If the prefix is not declared, or if the name is not valid, it'll either die or return undef depending on the current setting of "fatal_errors".
All methods of the interface have an alias that is the name used in the original Java specification. You can use either name interchangeably. Here is 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
- add more tests - optimise here and there
Закладки на сайте Проследить за страницей |
Created 1996-2024 by Maxim Chirkov Добавить, Поддержать, Вебмастеру |