aqua has asked for the wisdom of the Perl Monks concerning the following question:
I am making use of namespace based xml,having 5-6 namespaces.I am supposed to replace text of a particular namespace variable.I need to change VendorUUID or ProductRID or annotation. My programs displays localnamespaces (like ovf , vsm, vasd, rasd etc) of the namespaces. But, i am unable to make use of them to fetch or set data (replace) values of element. I need one function that will replace my text. The import thing here is , i am making use of $xc which is of XML::LibXML::XPathContext type. it no more supports Libxml. So, I need one function from XPathContext module, which will replace text and save the changed xml back. I have edited xml, you need to scroll horizontally, to get proper xml.
Also, I need to save to modfied xml back as xml copy
My profile.xml looks like
<?xml version="1.0" encoding="utf-8"?> <ovf:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance x +mlns:vadk="http://www.vmware.com/schema/vadk" xmlns:ovf="http://sch +emas.dmtf.org/ovf/envelope/1" xmlns:vssd="http://schemas.dmtf.org/w +bem/wscim/1/cim-schema/2/CIM_VirtualSystemSettingData" xmlns:rasd +="http://schemas.dmtf.org/wbem/wscim/1/cim- schema/2/CIM_ResourceAll +ocationSettingData" xmlns:vmw="http://www.vmware.com/schema/ovf" xm +lns:vsm="http://www.vmware.com/schema/vServiceManager" xsi:schemaLo +cation="http://schemas.dmtf.org/ovf/envelope/1 /opt/vmware/etc/build/ +schemas/vadk.xsd" ovf:version="0.7.0" vadk:version="2.6.0.0"> <References> <File ovf:href="system.vmdk" ovf:id="system.vmdk_id"/> </References> <Section xsi:type="ovf:AnnotationSection_Type"> <Info/> <Annotation>I am a test application</Annotation> </Section> <Section xsi:type="ovf:ProductSection_Type" ovf:required="false"> <Info>VM ISV branding information</Info> <Product>Myprodoct-11.10</Product> <Vendor>Some Company</Vendor> <Version>11.10.0.24</Version> <FullVersion>11.10.0.24</FullVersion> <ProductUrl></ProductUrl> <VendorUrl>http://www.hp.com</VendorUrl> <AppUrl></AppUrl> <Category>Application</Category> </Section> <Section xsi:type="vadk:ExtendedProductSection_Type"> <Info/> <vadk:VendorUUID>4c555gh-67yh-49987c-a7ed-21345yght94b</va +dk:VendorUUID> <vadk:VendorUUID>another product UUID shubhra</vadk:Vendor +UUID> <vadk:ProductRID>71ab92ef-b47e-47ea-8e5a-0d76b70aacc4</vad +k:ProductRID> <vadk:ProductRID>333334444444444I-am-khan-product-RID</vad +k:ProductRID> <vadk:AppUrlName></vadk:AppUrlName> <vadk:Logo></vadk:Logo> <vadk:LogoUrl></vadk:LogoUrl> </Section> </ovf:Envelope>
My perl code looks like:
#!/usr/bin/perl use strict ; use warnings ; use XML::LibXML ; use XML::LibXML::XPathContext; #use File::Slurp; my $parser = XML::LibXML->new() ; my $doc ; my $nameSpaces = {} ; my $resultHash = {} ; my @nodes; my $node; my $owner; my $xpaths = { #queryOne => '//info/app/name', #queryTwo => '//info/app/owner', #queryThree => '//info/app/description', queryFour =>'//ovf:Envelope/Section/Annotation', queryFive =>'//Section/Product', querysix =>'//vadk:ProductRID', queryseven => '//ovf:Envelope/Section/vadk:ProductRID', queryeight => '//ovf:Envelope/Section/vadk:ProductRID', } ; eval{ $doc = $parser->parse_file( '/root/shubhra/myapp/profile.xml' ) ; }; if( $@ ){ print "Parsing error $@" ; } my $xc = XML::LibXML::XPathContext->new( $doc->documentElement() ) ; # I put them into a hash to remove duplicates - probably un-nessasary foreach my $node ( $doc->findnodes( '//*/namespace::*' ) ) { $nameSpaces->{ $node->getLocalName() } = $node->getValue() ; #print "\n local name :",$node->getLocalName(),"\n"; #print "\n node value :",$node->getValue(),"\n"; } for my $ns ( keys %{ $nameSpaces } ){ $xc->registerNs( $ns => $nameSpaces->{ $ns } ) ; print "\n $ns"; print "\n $nameSpaces->{ $ns }"; } my $result="London university"; print "\n Query results :\n \n"; foreach my $query ( keys %$xpaths ){ print "\n Inside Loop"; foreach my $node ($xc->findnodes( $xpaths->{ $query } )) { print "\n INNER LOOP"; $node->appendText("hello world"); print $node->textContent,"\n"; } } $xc->to_String; #not working
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How to make use of dynamic namespace registration to replace the values of elements and attributes using XML::LibXML::XPathContext xml module
by roboticus (Chancellor) on Sep 16, 2013 at 12:26 UTC | |
|
Re: How to make use of dynamic namespace registration to replace the values of elements and attributes using XML::LibXML::XPathContext xml module
by Anonymous Monk on Sep 16, 2013 at 06:20 UTC | |