________________________________
 From: Lawrence Statton <lawre...@cluon.com>
To: Irfan Sayed <irfan_sayed2...@yahoo.com> 
Cc: "beginners@perl.org" <beginners@perl.org> 
Sent: Thursday, August 16, 2012 7:28 PM
Subject: Re: xml parsing
 

Okay -- I've looked at the attachment -- remember when I mentioned namespaces a 
while back?  This document uses one, so things get more complicated.

The solution I always use for this is to put the root element into an XPath 
Context object and assign a prefix for the default namespace (I want oh-so-much 
to believe there's an easier solution than this, but I found this one late at 
night some years ago and have stuck with it ever since)

This code will do what you want:

#!/usr/bin/perl
use strict;
use warnings;
use XML::LibXML;
use XML::LibXML::XPathContext;

my $XML = '/tmp/carrerabuild1.proj';

# this loads the XML document, then passes the root element into the
# XPathContext constructor -- perldoc XML::LibXML::XPathContext

my $document = XML::LibXML::XPathContext->new(XML::LibXML->load_xml(location => 
$XML)->documentElement);


# this adds a prefix to the default namespace from your document so we
# can "find" its elements

$document->registerNs(p =>
  'http://schemas.microsoft.com/developer/msbuild/2003');

my @include = map $_->value(),
  $document->findnodes('//p:ItemGroup/p:BuildProject/@Include');


use Data::Dumper;
print Data::Dumper->Dump([\@include],[qw/*include/]);

truly awesome . it worked.
thanks. 

regards,
irfan

Reply via email to