in reply to Appending Text of One XML Node to that of the Other
To add to the list of solutions, this is what it would look like using XML::Rules:
use strict; use XML::Rules; my $filter = XML::Rules->new( style => 'filter', rules => { _default => 'raw', 'Key,Description' => 'raw extended', MsgSig => sub { my ($tag,$attrs) = @_; my $key = $attrs->{':Key'}{_content}; if ($key) { $attrs->{':Description'}{_content} .= " ($key)" } return $tag => $attrs; } } ); $filter->filter(\*DATA); __DATA__ <xdoc> <MsgSigs> <MsgSig> <Description>TSC1 - Torque/Speed Cntrl 1</Description> <Key>ln1</Key> <XtdFrame>True</XtdFrame> <NetworkKey>net0</NetworkKey> <MsgSignals> </MsgSignals> </MsgSig> <MsgSig> <Description>TSC1 - Torque/Speed Cntrl 1</Description> <Key>ln2</Key> <XtdFrame>True</XtdFrame> <NetworkKey>net0</NetworkKey> <MsgSignals> <Signal> <Description>0_SPN695 Eng. Override </Description> <Key>sig695</Key> <ValueType>1</ValueType> </Signal> </MsgSignals> </MsgSig> </MsgSigs> </xdoc>
It only keeps at most one <MsgSig> tag in memory using plain and simple Perl data structures. The rules specify what to do with the tags. 'raw' means 'just copy over', 'raw extended' means 'copy over, but also give me a shortcut' and the subroutine gets called for each <MsgSig> tag, reads the contents of its <Key> subtag and appends them to the contents of the <Description>subtag.
Jenda
Enoch was right!
Enjoy the last years of Rome.
|
|---|