P
placebo.domingo
I'm pulling my hair out trying to do something that seems like it ought
to be really simple to do. In a nutshell, all I want to do is process
an XML document using only the elements that are in a certain
namespace. Elements that are in any other namespace should be ignored
by my processor. It seems like a simple objective but, despite reading
the documentation on XML:arser and XML:arser::Expat I can't figure
out how to selectively ignore elements based on namespace.
Let me give an example of what I want to do. Consider the following
XML document:
<?xml version="1.0" encoding="UTF-8"?>
<nsa:note
xmlns:nsa="http://www.mydomain.org/NameSpaceA"
xmlns:nsb="http://www.mydomain.org/NameSpaceB"<nsa:data>
<nsb:mystuff/>
<mystuff xmlns="http://www.mydomain.org/NameSpaceB">
<details/>
</mystuff>
</nsa:data>
</nsa:note>
That document uses two namespaces: http://www.mydomain.org/NameSpaceA
and http://www.mydomain.org/NameSpaceB. Suppose that in my parser I
only care about the elements associated with
http://www.mydomain.org/NameSpaceA (which we'll call "nsa"). Elements
associated with http://www.mydomain.org/NameSpaceB or any other
namespace should be ignored.
I'm open to any suggestions on how to do this. Let me show how I've
been *trying* to do it and maybe you can tell me what to fill in.
Consider this very simple perl code:
package XML::SchemaInfo;
use strict;
use XML:arser;
my ($p);
$p = XML:arser->new(Namespaces=>1);
$p->setHandlers(Start => \&StartTag);
$p->parsefile('my.xml');
sub StartTag {
my ($expat, $tagname) = @_;
my $namespace = "HOW DO I DO THIS???";
print "$tagname: this tag is in the namespace $namespace\n";
}
In the StartTag subroutine, $tagname is input as the name of the tag
without any namespace prefix. Well and good, but I want to know the
namespace.
See that line that says "HOW DO I DO THIS???" That's where I would
want some kind of call that returns the namespace for the tag that
triggered the call to the handler. I frankly admit I thought there
would be some kind of method call called "tag_namespace" or something
like that.
I've read and reread the document Perldocs for XML:arser and
XML:arser::Expat and I just don't see anything that tells me how to
tell the namespace of an element.
Any help would be appreciated.
to be really simple to do. In a nutshell, all I want to do is process
an XML document using only the elements that are in a certain
namespace. Elements that are in any other namespace should be ignored
by my processor. It seems like a simple objective but, despite reading
the documentation on XML:arser and XML:arser::Expat I can't figure
out how to selectively ignore elements based on namespace.
Let me give an example of what I want to do. Consider the following
XML document:
<?xml version="1.0" encoding="UTF-8"?>
<nsa:note
xmlns:nsa="http://www.mydomain.org/NameSpaceA"
xmlns:nsb="http://www.mydomain.org/NameSpaceB"<nsa:data>
<nsb:mystuff/>
<mystuff xmlns="http://www.mydomain.org/NameSpaceB">
<details/>
</mystuff>
</nsa:data>
</nsa:note>
That document uses two namespaces: http://www.mydomain.org/NameSpaceA
and http://www.mydomain.org/NameSpaceB. Suppose that in my parser I
only care about the elements associated with
http://www.mydomain.org/NameSpaceA (which we'll call "nsa"). Elements
associated with http://www.mydomain.org/NameSpaceB or any other
namespace should be ignored.
I'm open to any suggestions on how to do this. Let me show how I've
been *trying* to do it and maybe you can tell me what to fill in.
Consider this very simple perl code:
package XML::SchemaInfo;
use strict;
use XML:arser;
my ($p);
$p = XML:arser->new(Namespaces=>1);
$p->setHandlers(Start => \&StartTag);
$p->parsefile('my.xml');
sub StartTag {
my ($expat, $tagname) = @_;
my $namespace = "HOW DO I DO THIS???";
print "$tagname: this tag is in the namespace $namespace\n";
}
In the StartTag subroutine, $tagname is input as the name of the tag
without any namespace prefix. Well and good, but I want to know the
namespace.
See that line that says "HOW DO I DO THIS???" That's where I would
want some kind of call that returns the namespace for the tag that
triggered the call to the handler. I frankly admit I thought there
would be some kind of method call called "tag_namespace" or something
like that.
I've read and reread the document Perldocs for XML:arser and
XML:arser::Expat and I just don't see anything that tells me how to
tell the namespace of an element.
Any help would be appreciated.