ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> perl-xml
perl-xml
XML::LibXML::XPathContext namespace confusion
by Scott Stansbury other posts by this author
Sep 9 2009 3:09PM messages near this date
view in the new Beta List Site
RE: parsing a huge xml document | Re: XML::LibXML::XPathContext namespace confusion
& XSLT 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/s
chema/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...
Thread:
Scott Stansbury
Grant McLean
Richard E. Rathmann

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved