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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.