Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > How do I modify version number in XML doc - path to pass to Twig?

Reply
Thread Tools

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

 
 
Sherman Willden
Guest
Posts: n/a
 
      08-06-2003
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
 
Reply With Quote
 
 
 
 
Michel Rodriguez
Guest
Posts: n/a
 
      08-07-2003
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
 
Reply With Quote
 
 
 
 
sherman willden
Guest
Posts: n/a
 
      08-07-2003
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!
 
Reply With Quote
 
Sherman Willden
Guest
Posts: n/a
 
      08-08-2003
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); }
}
 
Reply With Quote
 
Sherman Willden
Guest
Posts: n/a
 
      08-08-2003
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


http://www.velocityreviews.com/forums/(E-Mail Removed) (Sherman Willden) wrote in message news:<(E-Mail Removed) 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); }
> }

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Where to get stand alone Dot Net Framework version 1.1, version2.0, version 3.0, version 3.5, version 2.0 SP1, version 3.0 SP1 ? MowGreen [MVP] ASP .Net 5 02-09-2008 01:55 AM
Re: Where to get stand alone Dot Net Framework version 1.1, version 2.0, version 3.0, version 3.5, version 2.0 SP1, version 3.0 SP1 ? PA Bear [MS MVP] ASP .Net 0 02-05-2008 03:28 AM
Re: Where to get stand alone Dot Net Framework version 1.1, version 2.0, version 3.0, version 3.5, version 2.0 SP1, version 3.0 SP1 ? V Green ASP .Net 0 02-05-2008 02:45 AM
String[] files = {"a.doc, b.doc"}; VERSUS String[] files = new String[] {"a.doc, b.doc"}; Matt Java 3 09-17-2004 10:28 PM
How do I modify version number in XML doc - path to pass to Twig? Sherman Willden Perl Misc 3 08-08-2003 08:43 PM



Advertisments