Re: XML::LibXML::XPathContext namespace confusion
"Richard E. Rathmann" <[email protected]>
| Newsgroups | gmane.comp.lang.perl.xml |
|---|---|
| Message-ID | <[email protected]> |
The problem is most likely the default namespace
(http://scap.nist.gov/schema/feed/vulnerability/2.0), which you need to
register with a prefix and then use that prefix in any XPath references
to nodes in this namespace (e.g., the "nvd" and "entry" in
$xp->findnodes("/nvd/entry[\@id = '$cve_id']")).
HTH,
Richard
Scott Stansbury wrote:
> I trying to put together a script that searches an XML file for a
> specific entry, and then returns children of that node. I have it
> working with XML::XPath, but it seems slower than I would like.
>
> I am confused with the namespace issues using XML::LibXML::XPathContext.
>
> The files that I am searching/parsing are the NIST's NVD CVE files.
> The header lists multiple namespaces (to my novice eye, anyway...):
>
> <?xml version='1.0' encoding='UTF-8'?>
> <nvd xmlns="http://scap.nist.gov/schema/feed/vulnerability/2.0"
> xmlns:cvss="http://scap.nist.gov/schema/cvss-v2/0.2"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xmlns:vuln="http://scap.nist.gov/schema/vulnerability/0.4"
> xmlns:cpe-lang="http://cpe.mitre.org/language/2.0"
> nvd_xml_version="2.0" pub_date="2009-01-28T03:30:00"
> xsi:schemaLocation="http://scap.nist.gov/schema/feed/vulnerability/2.0
> http://nvd.nist.gov/schema/nvd-cve-feed_2.0.xsd">
>
> The entries look like this:
> <entry id="CVE-2006-0069">
> <vuln:vulnerable-configuration id="http://nvd.nist.gov">
> <cpe-lang:logical-test negate="false" operator="OR">
> <cpe-lang:fact-ref
> name="cpe:/a:chipmunk_scripts:chipmunk_guestbook:1.4" />
> </cpe-lang:logical-test>
> </vuln:vulnerable-configuration>
> <vuln:vulnerable-software-list>
>
> <vuln:product>cpe:/a:chipmunk_scripts:chipmunk_guestbook:1.4</vuln:product>
> </vuln:vulnerable-software-list>
> <vuln:cve-id>CVE-2006-0069</vuln:cve-id>
>
> <vuln:published-datetime>2006-01-03T18:03:00.000-05:00</vuln:published-datetime>
>
> <vuln:last-modified-datetime>2008-09-05T16:58:16.357-04:00</vuln:last-modified-datetime>
> <vuln:cvss>
> <cvss:base_metrics upgraded-from-version="1.0">
> <cvss:score>4.3</cvss:score>
> <cvss:access-vector
> approximated="true">NETWORK</cvss:access-vector>
> <cvss:access-complexity>MEDIUM</cvss:access-complexity>
> <cvss:authentication>NONE</cvss:authentication>
>
> <cvss:confidentiality-impact>NONE</cvss:confidentiality-impact>
> <cvss:integrity-impact>PARTIAL</cvss:integrity-impact>
> <cvss:availability-impact>NONE</cvss:availability-impact>
> <cvss:source>http://nvd.nist.gov</cvss:source>
>
> <cvss:generated-on-datetime>2006-01-04T08:48:00.000-05:00</cvss:generated-on-datetime>
> </cvss:base_metrics>
> </vuln:cvss>
>
> this carries on, but what I'm interested in is in this chunk.
>
> The code looks like this:
>
> #!/usr/bin/perl -w
>
> use strict;
> use warnings;
>
> # use XML::XPath;
> # use XML::XPath::XMLParser;
> use XML::LibXML;
> use XML::LibXML::XPathContext;
>
> die "Usage: $0 CVE Identifier\n" unless 1 == @ARGV;
>
> # my $cve_id = 'CVE-2008-3763';
> my $cve_id = $ARGV[0];
>
> # parse the CVE identifier to determine what data file to search
> my @id_fields = split(/-/,$cve_id);
> my $year = $id_fields[1];
>
> my $data_file = "./nvdcve-2.0-" . $year . ".xml";
>
> my $xp = XML::LibXML::XPathContext-> new($data_file);
>
> for my $entry ($xp->findnodes("/nvd/entry[\@id = '$cve_id']")) {
> my ($metrics) = $entry->findnodes('vuln:cvss/cvss:base_metrics');
>
> my $av = $metrics->find('cvss:access-vector');
> my $ac = $metrics->find('cvss:access-complexity');
> my $au = $metrics->find('cvss:authentication');
> my $ci = $metrics->find('cvss:confidentiality-impact');
> my $ii = $metrics->find('cvss:integrity-impact');
> my $ai = $metrics->find('cvss:availability-impact');
>
> print $av . "\n";
> print $ac . "\n";
> print $au . "\n";
> print $ci . "\n";
> print $ii . "\n";
> print $ai . "\n";
> last;
> }
>
> What do I register as a namespace to make things work?
>
> As always, thanks for any help!
>
> Scott...
>
>
>
>
> =
> ------------------------------------------------------------------------
>
> _______________________________________________
> Perl-XML mailing list
> [email protected]
> To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
>
_______________________________________________
Perl-XML mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs