S
Sherman Willden
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
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