You want to paste the element at the end of the proper classes element.
It can be a bit of a pain to write because of the interaction between the quotes in
Perl and in XPath (and the fact that @ means something different in both languages), so
here is a way to do it:
my $class_name= "Nursery";
my $class= $root->first_child( qq{classes[\@name="$class_name"]});
$node->paste( last_child => $class);
Just in case you don't know this: qq{...} is an alternate way of writing "...", and the
@ needs to be backslashed or Perl sees it as an array to be interpolated in the
string.
A couple of comments: classes seems weird as an element name, as it looks like it
contains only 1 class, so why the plural? Instead of $className = $element->{'att'}-*gt;{"name"};
you should really write $className = $element->att( "name"); . This is the "official"
way to access attributes, and even though what you wrote works today there is nothing in the
published API for XML::Twig that garantees that it will work in the future (ie I could possibly
change the implementation of attributes).
Does this help?