3

This is my sample xml file (snipped version of deployment plan for weblogic application).

<?xml version="1.0" encoding="UTF-8"?>
<deployment-plan>
  <application-name>ear-my-service</application-name>
  <variable-definition>
    <variable>
      <name>characteristics</name>
      <value>myTestEAR</value>
    </variable>
    <variable>
      <name>Url</name>
      <value>ABC</value>
    </variable>
    <variable>
      <name>time</name>
      <value>300</value>
    </variable>
  </variable-definition>
</deployment-plan>

I want to update the value when the name is characteristics. I looked around and put together this script.

#!/usr/bin/perl

use strict;
use warnings;
use XML::Twig;

my $data = shift ||die $!;

my $t= XML::Twig->new(
    twig_handlers => {
        q{project[string(name) =~ /\bcharacteristics\b/]/value} => \&value,
    },
    pretty_print => 'indented',
);
$t->parsefile( $data );
$t->print;

sub value {
    my ($twig, $value) = @_;
    $value->set_text("myTestEAR_Modified");
}

However, it doesn't change the value to myTestEAR_Modified. Am I doing it incorrectly?

1 Answer 1

2

The tag name in the XML is variable, not project, replace it in the condition (q{variable[string(name)...) and it will work.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks a lot, Mirod, that was indeed the problem.
Also, how could I add more than one checks? Example, if name is equal to characteristics, set the value to myTestEAR_Modified and if name is equal to Url, set the value to UrlModified?
you could either have several conditions, each with its own handler, or just set a handler on variable, and update the value (in $variable->first_child( 'value')) depending on the name (in $variable->first_child( 'name'))
Thanks, I tried the latter and it worked. One last question - instead of printing the xml with the replacements, I would like to edit the original xml in line. How could that be done?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.