473,387 Members | 1,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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_scripts\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::XMLParser;
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_xml" );

# 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::XMLParser::as_string( $node ) . "\n";
#}

my $new_value = 2.20.0.12;
my $t = new XML::Twig( TwigRoots =>
{ $field[string() = $old_value] => \&update },
TwigPrintOutsideRoots => 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
<InstallAnywhere_Deployment_Project increments="nnnn">
<essentialScriptInfo>
<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>
<authorizationID>lots of numbers removed</authorizationID>
</essentialScriptInfo>
<installationObjects uniqueObjects="307">
<object class="com.zerog.ia.installer.Installer" objectID="some
numbers removed">
<property name="classpath">
<object class="java.util.Vector"/>
</property>
lots of stuff removed
<property name="RPMSpec">
<object class="com.zerog.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="description">
<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="distribution">
<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="buildSettings">
<object class="java.util.Properties">

Lots of stuff to end of doucument removed
Jul 20 '05 #1
4 3216
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 },
TwigPrintOutsideRoots => 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_tmp.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::XMLParser;
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.google.c om>...
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_tmp.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::XMLParser;
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
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...
0
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...
1
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...
5
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...
1
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...
0
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
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...
0
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
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....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.