I'm trying to query an XML record based on an attribute, and then return child elements of that record.
This is the code, eventually this will be a subroutine that the query string will be fed to.
use strict; use warnings; use XML::XPath; use XML::XPath::XMLParser; my $data_file = "./nvdcve-2.0-2008.xml"; my $cve_id = 'CVE-2008-3763'; my $query = "/nvd/entry[@id = $cve_id]"; my $xp = XML::XPath-> new(filename => "$data_file"); foreach my $cve_entry ($xp->findnodes($query)) { my $AV = $cve_entry->find('//entry/vuln:cvss/cvss:base_metrics +/cvss:access-vector'); my $AC = $cve_entry->find('//entry/vuln:cvss/cvss:base_metrics +/cvss:access-vector'); my $AV = $cve_entry->find('//entry/vuln:cvss/cvss:base_metrics +/cvss:access-complexity'); # etc... }
The chunk of the source file looks like this:
<?xml version='1.0' encoding='UTF-8'?> <nvd xmlns:cpe-lang="http://cpe.mitre.org/language/2.0" xmlns:vuln="ht +tp://scap.nist.gov/schema/vulnerability/0.4" xmlns:cvss="http://scap. +nist.gov/schema/cvss-v2/0.2" xmlns:xsi="http://www.w3.org/2001/XMLSch +ema-instance" xmlns="http://scap.nist.gov/schema/feed/vulnerability/2 +.0" nvd_xml_version="2.0" pub_date="2009-01-28T03:10:00" xsi:schemaLo +cation="http://scap.nist.gov/schema/feed/vulnerability/2.0 http://nvd +.nist.gov/schema/nvd-cve-feed_2.0.xsd"> <entry id="CVE-2008-3763"> <vuln:cvss> <cvss:base_metrics> <cvss:score>6.8</cvss:score> <cvss:access-vector>NETWORK</cvss:access-vector> <cvss:access-complexity>MEDIUM</cvss:access-complexity> <cvss:authentication>NONE</cvss:authentication> <cvss:confidentiality-impact>PARTIAL</cvss:confidentiality +-impact> <cvss:integrity-impact>PARTIAL</cvss:integrity-impact> <cvss:availability-impact>PARTIAL</cvss:availability-impac +t> <cvss:source>http://nvd.nist.gov</cvss:source> cvss:generated-on-datetime>2008-08-21T14:37:00.000-04:00</ +cvss:generated-on-datetime> </cvss:base_metrics> </vuln:cvss> </entry>
I get "invalid attribute" or other errors - the "@" is not being recognized as a descriptor for the attribute, it seems...
In addition, am I OK with not set the namespace? From the XML::XPath page at CPAN:
set_namespace($prefix, $uri)
Sets the namespace prefix mapping to the uri.
Normally in XML::XPath the prefixes in XPath node tests take their context from the current node. This means that foo:bar will always match an element <foo:bar> regardless of the namespace that the prefix foo is mapped to (which might even change within the document, resulting in unexpected results). In order to make prefixes in XPath node tests actually map to a real URI, you need to enable that via a call to the set_namespace method of your XML::XPath object.
As always, thanks for your time... Scott
In reply to XPath query issue... by spstansbury
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |