Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^2: 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:43 UTC ( #1054234=note: print w/ replies, xml ) Need Help??


in reply to Re: How to make use of dynamic namespace registration to replace the values of elements and attributes using XML::LibXML::XPathContext xml module
in thread How to make use of dynamic namespace registration to replace the values of elements and attributes using XML::LibXML::XPathContext xml module

Aside from obvious (I assume copy/paste/fatfinger ) typos in your xml, I don't see why you'd have to register any namespaces through code dynamically or otherwise as the xml appears to do adequate registration itself early enough to be useful :)

$xc->to_String; #not working

$xc is https://metacpan.org/module/XML::LibXML::XPathContext not a https://metacpan.org/module/XML::LibXML::Node which only has a "toString"

works for me

#!/usr/bin/perl -- use strict; use warnings; use XML::LibXML; my $xmlin = q{<?xml version="1.0" encoding="utf-8"?> <ovf:Envelope ovf:version="0.7.0" vadk:version="2.6.0.0" xmlns:ovf="http://schemas.dmtf.org/ovf/envelope/1" xmlns:rasd="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_Reso +urceAllocationSettingData" xmlns:vadk="http://www.vmware.com/schema/vadk" xmlns:vmw="http://www.vmware.com/schema/ovf" xmlns:vsm="http://www.vmware.com/schema/vServiceManager" xmlns:vssd="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_Virt +ualSystemSettingData" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://schemas.dmtf.org/ovf/envelope/1 /opt/vmware +/etc/build/schemas/vadk.xsd"> <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 ovf:required="false" xsi:type="ovf:ProductSection_Type"> <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</vadk:Vendo +rUUID> <vadk:VendorUUID>another product UUID shubhra</vadk:VendorUUID> <vadk:ProductRID>71ab92ef-b47e-47ea-8e5a-0d76b70aacc4</vadk:Produc +tRID> <vadk:ProductRID>333334444444444I-am-khan-product-RID</vadk:Produc +tRID> <vadk:AppUrlName></vadk:AppUrlName> <vadk:Logo></vadk:Logo> <vadk:LogoUrl></vadk:LogoUrl> </Section> </ovf:Envelope> }; my $doc = XML::LibXML->new()->load_xml( string => $xmlin ); for my $node ( $doc->findnodes('//vadk:VendorUUID | //vadk:ProductRID + | //Annotation') ){ print $node->nodePath,"\n"; $node->appendText(" ##6##6##6## "); } print $doc; if(0) { ##https://metacpan.org/module/XML::LibXML#ENCODINGS-SUPPORT-IN-XML::LI +BXML use autodie; open my($outfh), '>:raw', 'outfile.xml'; $doc->toFH( $outfh ); close $outfh; } __END__ /ovf:Envelope/Section[1]/Annotation /ovf:Envelope/Section[3]/vadk:VendorUUID[1] /ovf:Envelope/Section[3]/vadk:VendorUUID[2] /ovf:Envelope/Section[3]/vadk:ProductRID[1] /ovf:Envelope/Section[3]/vadk:ProductRID[2] <?xml version="1.0" encoding="utf-8"?> <ovf:Envelope xmlns:ovf="http://schemas.dmtf.org/ovf/envelope/1" xmlns +:rasd="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_Resource +AllocationSettingData" xmlns:vadk="http://www.vmware.com/schema/vadk" + xmlns:vmw="http://www.vmware.com/schema/ovf" xmlns:vsm="http://www.v +mware.com/schema/vServiceManager" xmlns:vssd="http://schemas.dmtf.org +/wbem/wscim/1/cim-schema/2/CIM_VirtualSystemSettingData" xmlns:xsi="h +ttp://www.w3.org/2001/XMLSchema-instance" ovf:version="0.7.0" vadk:ve +rsion="2.6.0.0" xsi:schemaLocation="http://schemas.dmtf.org/ovf/envel +ope/1 /opt/vmware/etc/build/schemas/vadk.xsd"> <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 ##6##6##6## </Annotation> </Section> <Section ovf:required="false" xsi:type="ovf:ProductSection_Type"> <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/> <VendorUrl>http://www.hp.com</VendorUrl> <AppUrl/> <Category>Application</Category> </Section> <Section xsi:type="vadk:ExtendedProductSection_Type"> <Info/> <vadk:VendorUUID>4c555gh-67yh-49987c-a7ed-21345yght94b ##6##6##6## + </vadk:VendorUUID> <vadk:VendorUUID>another product UUID shubhra ##6##6##6## </vadk:V +endorUUID> <vadk:ProductRID>71ab92ef-b47e-47ea-8e5a-0d76b70aacc4 ##6##6##6## +</vadk:ProductRID> <vadk:ProductRID>333334444444444I-am-khan-product-RID ##6##6##6## +</vadk:ProductRID> <vadk:AppUrlName/> <vadk:Logo/> <vadk:LogoUrl/> </Section> </ovf:Envelope>

See also perlintro, open, binmode, [doc://

See also

Re: How to parse xml with namespase vale in XMl:LibXML? ( XPath error : Undefined namespace prefix )

Re^2: How to write an xpath query including attributes for an XML with namespace

Re: XML::LibXML and namespaces, Re^2: Namespaced XML::LibXML XPath query (not a bug)

The XML FIles: Understanding XML Namespaces, Re: XML::LibXSLT ignoring namespace on some Win32 machines (libxslt-1.1.27 bug), Re^2: XML::LibXML and namespaces, Re^2: XML::LibXML and namespaces, Re: XML::LibXML and namespaces, XPATH DOM traverse html/xml,


Comment on Re^2: How to make use of dynamic namespace registration to replace the values of elements and attributes using XML::LibXML::XPathContext xml module
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1054234]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2015-07-06 00:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (68 votes), past polls