473,769 Members | 6,126 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do I modify version number in XML doc - path to pass to Twig?

I am trying to use Perl's XML::Twig to modify a version number in an
XML document. At the very end of this posting is an excerpt from the
xml document. Just before the xml excerpt is the Perl code I am trying
to use. The Xpath part of the script functions well.

I think I need help determining the @field entry. I have tried
//property[\@name='version ']/string, version/string, and string. I get
a "Can't modify non-lvalue subroutine call at C:\business
copy\xml_try_sc ripts\try_xpath .pl line 26." Error which I think is
caused by $field[] being null.

Any ideas about what the @field value should be?

Thanks;

Sherman

#!/usr/bin/perl -w

use strict;
use XML::XPath;
use XML::XPath::XML Parser;
use XML::Twig;

# create an object to parse the file and field XPath queries
# my $xpath = XML::XPath->new( filename => shift @ARGV );
my $xpath = XML::XPath->new( filename => "Product.iap_xm l" );

# apply the path from the command line and get back a list matches
my $field;
my @field = "string";

my $old_value = $xpath->find(
"//property[\@name='version ']/string/text()" );
## $old_value returns 2.20.0.0
# print each node in the list
# foreach my $node ( $old_value->get_nodelist ) {
# print XML::XPath::XML Parser::as_stri ng( $node ) . "\n";
#}

my $new_value = 2.20.0.12;
my $t = new XML::Twig( TwigRoots =>
{ $field[string() = $old_value] => \&update },
TwigPrintOutsid eRoots => 1,);
$t->parsefile( 'BC_HA.iap_xml' );
$t->flush;

sub update
{
my( $t, $field_elt)= @_;
$field_elt->set_text( $new_value);
$field_elt->print;
}
## End of perl script and start of XML document

<?xml version="1.0" encoding="UTF-8"?>
Some comment stuff removed
<InstallAnywher e_Deployment_Pr oject increments="nnn n">
<essentialScrip tInfo>
<versionID major="n" minor="n" revision="-1"/>
<editionID> lots of numbers removed </editionID>
<scriptID> lots of numbers removed </scriptID>
<buildID> lots of numbers removed </buildID>
<authorizationI D>lots of numbers removed</authorizationID >
</essentialScript Info>
<installationOb jects uniqueObjects=" 307">
<object class="com.zero g.ia.installer. Installer" objectID="some
numbers removed">
<property name="classpath ">
<object class="java.uti l.Vector"/>
</property>
lots of stuff removed
<property name="RPMSpec">
<object class="com.zero g.ia.installer. RPMSpec" objectID="some
numbers removed">
<property name="enabled">
<boolean>true </boolean>
</property>
<property name="name">
<string><![CDATA[Our Product 2.2 Product Sub Name]]></string>
</property>
<property name="version">
<string><![CDATA[2.20.0.0]]></string>
</property>
<property name="release">
<string><![CDATA[2.20.0.0]]></string>
</property>
<property name="descripti on">
<string><![CDATA[]]></string>
</property>
<property name="summary">
<string><![CDATA[<none>]]></string>
</property>
<property name="copyright ">
<string><![CDATA[2003]]></string>
</property>
<property name="url">
<string><![CDATA[http://www.this.com]]></string>
</property>
<property name="distribut ion">
<string><![CDATA[<none>]]></string>
</property>
<property name="vendor">
<string><![CDATA[HP]]></string>
</property>
<property name="group">
<string><![CDATA[Applications/System]]></string>
</property>
<property name="packager" >
<string><![CDATA[]]></string>
</property>
</object>
</property>
<property name="buildSett ings">
<object class="java.uti l.Properties">

Lots of stuff to end of doucument removed
Jul 20 '05 #1
4 3237
Sherman Willden wrote:
I am trying to use Perl's XML::Twig to modify a version number in an
XML document. At the very end of this posting is an excerpt from the
xml document. Just before the xml excerpt is the Perl code I am trying
to use. The Xpath part of the script functions well. my $new_value = 2.20.0.12;
my $t = new XML::Twig( TwigRoots =>
{ $field[string() = $old_value] => \&update },
TwigPrintOutsid eRoots => 1,);
$t->parsefile( 'BC_HA.iap_xml' );
$t->flush;


I think the problem comes from the fact that $field[string() = $old_value]
is not quoted, which means that it will be executed, and string()=
$old_value will give the error message you get.

The XPath expression is a simple string, which needs to be quoted, the
attribute value needs to be quoted in the expression, and finally you need
to escap the [ after $field or Perl thinks that field is an array.

qq{$field\[string() = "$old_value "]}

That said, this won't work either: twig_roots needs to be able to determine
whether an element will be processed right when the element start, not
later when you can use the string() value.

The best you can do is probably to use $field for the twig_roots and then
test on the content in the handler.

Here is a version that builds the tree for all elements, but that flushes
them as soon as possible:
my $field= 'string';
my $old_value = '2.20.0.0';
my $new_value = '2.20.0.12';

my $t = new XML::Twig( twig_handlers =>
{ qq{$field\[string() = "$old_value "]} => \&update, # process
__default__ => sub { $_[0]->flush; }, # flush anything else
},
pretty_print => 'record_c',
);
$t->parse( \*DATA );
$t->flush;

sub update
{
my( $t, $field_elt)= @_;
# if you want the version number to be in a CDATA section
# then set_content is not enough
if( my $cdata= $field_elt->first_child_is ( '#CDATA'))
{ $cdata->set_cdata( $new_value); }
else
{ $field_elt->set_text( $new_value); }
}
Michel Rodriguez
Perl &amp; XML
http://xmltwig.com
Jul 20 '05 #2
Thank you, Michael. I will try this today.

Sherman

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #3
Thanks, Michael, for your response earlier and the code works, the
values are modified, and the text stream is output to the screen.

My question is how do I get a well-formed XML document from the
results? I tried printing to a file handle ( C:\temp\twig_tm p.xml )
but that didn't work. Below is the code that outputs the results to
the screen.

To further explain, I have file xyz.xml. I want to modify one value in
the xyz.xml file and have a working xyz.xml file after the
modification.

I am sure that I missed the answer when reading Michael's site, Perl
and XML, and the twig documentation. Can someone provide a reference
to the paragraph(s) that explains what I missed?

Thanks;

Sherman

#!/usr/bin/perl -w

use strict;
use XML::XPath;
use XML::XPath::XML Parser;
use XML::Twig;

my $field= 'string';
my $old_value = '2.20.0.0';
my $new_value = '2.20.0.12';

if ( -f "C:/temp/twig_tmp.xml" ) {
chmod(0777, "C:/temp/twig_tmp.xml");
unlink("C:/temp/twig_tmp.xml");
}

#open(TMPFLE, ">>c:/temp/twig_tmp.xml");

my $t = new XML::Twig( twig_handlers =>
{ qq{$field\[string() = "$old_value "] } => \&update, # process
__default__ => sub { $_[0]->flush; }, # flush anything
else
},
pretty_print => 'record_c',);
$t->parsefile( 'BC_HA.iap_xml' );
#$t->print( \*TMPFLE );
$t->flush;

#close(TMPFLE);

sub update
{
my( $t, $field_elt)= @_;
# if you want the version number to be in a CDATA section
# then set_content is not enough
if( my $cdata= $field_elt->first_child_is ( '#CDATA'))
{ $cdata->set_cdata( $new_value); }
else
{ $field_elt->set_text( $new_value); }
}
Jul 20 '05 #4
I have it now and my apologies to Michel Rodriguez if I gave any
indication that it didn't work.

I was comparing the look and file size of the output file which were
different from the original file since I was using record_c. After
posting this I ran a parser and the file was xml compliant. After I
used indented_c the file size and appearance was the same as the
original file.

Sherman
sh************* @hp.com (Sherman Willden) wrote in message news:<3a******* *************** ***@posting.goo gle.com>...
Thanks, Michael, for your response earlier and the code works, the
values are modified, and the text stream is output to the screen.

My question is how do I get a well-formed XML document from the
results? I tried printing to a file handle ( C:\temp\twig_tm p.xml )
but that didn't work. Below is the code that outputs the results to
the screen.

To further explain, I have file xyz.xml. I want to modify one value in
the xyz.xml file and have a working xyz.xml file after the
modification.

I am sure that I missed the answer when reading Michael's site, Perl
and XML, and the twig documentation. Can someone provide a reference
to the paragraph(s) that explains what I missed?

Thanks;

Sherman

#!/usr/bin/perl -w

use strict;
use XML::XPath;
use XML::XPath::XML Parser;
use XML::Twig;

my $field= 'string';
my $old_value = '2.20.0.0';
my $new_value = '2.20.0.12';

if ( -f "C:/temp/twig_tmp.xml" ) {
chmod(0777, "C:/temp/twig_tmp.xml");
unlink("C:/temp/twig_tmp.xml");
}

#open(TMPFLE, ">>c:/temp/twig_tmp.xml");

my $t = new XML::Twig( twig_handlers =>
{ qq{$field\[string() = "$old_value "] } => \&update, # process
__default__ => sub { $_[0]->flush; }, # flush anything
else
},
pretty_print => 'record_c',);
$t->parsefile( 'BC_HA.iap_xml' );
#$t->print( \*TMPFLE );
$t->flush;

#close(TMPFLE);

sub update
{
my( $t, $field_elt)= @_;
# if you want the version number to be in a CDATA section
# then set_content is not enough
if( my $cdata= $field_elt->first_child_is ( '#CDATA'))
{ $cdata->set_cdata( $new_value); }
else
{ $field_elt->set_text( $new_value); }
}

Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2416
by: Chris McKeever | last post by:
I am trying to modify the Mailman Python code to stop mapping MIME-types and use the extension of the attachment instead. I am pretty much clueless as to what I need to do here, but I think I have narrowed it down to the Scrubber.py file.. If this seems like a quick step me through, I would be very appreciative, could get you something on your Amazon wish-list (that is me on my knees begging).. From just my basic understanding, it...
0
2351
by: Anthony Baxter | last post by:
To go along with the 2.4a3 release, here's an updated version of the decorator PEP. It describes the state of decorators as they are in 2.4a3. PEP: 318 Title: Decorators for Functions and Methods Version: $Revision: 1.34 $ Last-Modified: $Date: 2004/09/03 09:32:50 $ Author: Kevin D. Smith, Jim Jewett, Skip Montanaro, Anthony Baxter
1
2062
by: LC's No-Spam Newsreading account | last post by:
I have the following arrangement working under Netscape 3 / Unix, IE6 / Win and Konqueror / Linux, but NOT under Netscape 7 Unix or Mozilla Linux (silently fails) nor under Netscape 4 Unix (fails with message "access disallowed from scripts at XXXX to documents at another domain"). 1) I have a frameset http://host/PATH/pinco,html in whose HEAD I define javascript functions like : function changeItem(i,v) {
5
4480
by: Martin Bischoff | last post by:
Hi, is it possible to modify the values of a SqlDataSource's select parameters in the code behind before the select command is executed? Example: I have an SqlDataSource with a ControlParameter <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:XYZ %>"
1
1928
by: Hexman | last post by:
Hello All, I have a new challenge. I've created a Crystal Report (version 10) and saved the file. Now from my program I want to print it to an Adobe PDF file. My question is: How do I pass parameters thru Crystal to modify printer behavior? For Instance: 1. Pass it a path & filename to save to. eg; "C:\ReportDist\R100102-060902.pdf" (included the date in the filename) 2. Tell the Adobe PDF printer to overwrite an existing file if...
0
1160
by: sugarboy | last post by:
Hi, Can u let me know how to construct an XML using XML twig in perl. Also can u help in finding good notes on XML twig. Thanks, Sugarboy
12
13274
by: vbnewbie | last post by:
I am having problems accessing properties of dynamically generated objects in VB2005. Can someone please help? In a nutshell: My app creates an equal number of checkboxes and labels that share the same Tag number. (I thought it might help) The checkboxes name is a concatenation of "chkCancel" and a number that represents the order in which they were created: chkCancel0 (Tag = 0) chkCancel1 (Tag = 1)
0
1033
by: skrishnaveni | last post by:
Hi, Suppose there is an xml with the following format: <people> <name1> <address1>abc</address1> <city1>abc</city1> </name1> <name2>
0
1771
by: =?Utf-8?B?RHVja3dvbg==?= | last post by:
Hello everyone, I need to get the version number of a couple of DLLs (comctl32.dll and msxml4.dll). I don't have the full path because I don't want to assume they will be in the system32 folder. I have a function which first does... //get the current process p = Process.GetCurrentProcess(); //get all the dlls this class is using
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10211
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9994
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7409
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5299
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3959
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.