/ / Perl libxml aggiorna il valore dell'attributo per l'ID univoco dato xml - perl, libxml2

Perl libxml aggiorna il valore dell'attributo per l'ID univoco dato xml - perl, libxml2

Ho bisogno di un aiuto per aggiornare gli attributi del file xml esistenti per l'ID univoco dato in xml,

Xml assomiglia a questo come input

 <TextLine>
<String ID="S14" CONTENT="Gun" SUBS_TYPE="HypPart1" SUBS_CONTENT="Gun"/>
</TextLine>
<TextLine>
<String ID="S15" CONTENT="nersen" SUBS_TYPE="HypPart1" SUBS_CONTENT="nersen"/>
</TextLine>

L'output è look

<TextLine>
<String ID="S14" CONTENT="Gun" SUBS_TYPE="HypPart1" SUBS_CONTENT="Gunnersen"/>
</TextLine>
<TextLine>
<String ID="S15" CONTENT="nersen" SUBS_TYPE="HypPart1" SUBS_CONTENT="Gunnersen"/>
</TextLine>

Sto aggiornando l'attributo SUBS_CONTENT.

Quando sono in loop Through String posso aggiornare l'elemento corrente, ma non ho il valore di stringa successivo, Dopo aver letto la riga successiva posso unire il Contenuto e posso inserirlo SUBS_CONTENT

Il mio codice sembra

foreach my $PAGE1 ($pagetext->findnodes("//String")){
my $sCurArt = $PAGE1->findvalue("@ID");
if ($sCurArt eq $id) {
my ($TextBlockIDx) = $PAGE1->findnodes("@SUBS_CONTENT");
$TextBlockIDx->setValue($text);
last;
}
}

Per favore aiutatemi su questo .....

C'è un modo per impostareValue con un dato xml ID (S14, S15).

Grazie in anticipo....

Umesh

risposte:

0 per risposta № 1

Non sono sicuro al 100% di aver compreso le tue specifiche, ma ecco cosa farei:

#!/usr/bin/perl
use warnings;
use strict;

use XML::LibXML;

my $xml = "XML::LibXML"->load_xml(IO => *DATA{IO});

my @ids = qw(S14 S15);

my @strings = map $xml->findnodes("//TextLine/String[@ID="" . $_ . ""]"), @ids;
my $new = join q(), map $_->findvalue("@SUBS_CONTENT"), @strings;
$_->setAttribute("SUBS_CONTENT", $new) for @strings;
print $xml->toString;

__DATA__
<r>
<TextLine>
<String ID="S14" CONTENT="Gun" SUBS_TYPE="HypPart1" SUBS_CONTENT="Gun"/>
</TextLine>
<TextLine>
<String ID="S15" CONTENT="nersen" SUBS_TYPE="HypPart1" SUBS_CONTENT="nersen"/>
</TextLine>
</r>

Recupero le stringhe parziali dall'XML, le aggiungo a $ nuove, quindi imposto $ nuove come nuovo valore degli attributi.


0 per risposta № 2

Sembra che tu abbia solo bisogno del setAttribute metodo in XML::LibXML::Element. C'è anche un getAttribute metodo.

use strict;
use warnings;

use XML::LibXML;

my $data = do { local $/; <DATA> };

my $doc = XML::LibXML->load_xml(string => $data);

for my $node ($doc->findnodes("//String[@ID="S14" or @ID="S15"]")) {
$node->setAttribute("SUBS_CONTENT" => "Gunnerson");
}

print $doc->toString();

__DATA__
<root>
<TextLine>
<String ID="S14" CONTENT="Gun" SUBS_TYPE="HypPart1" SUBS_CONTENT="Gun"/>
</TextLine>
<TextLine>
<String ID="S15" CONTENT="nersen" SUBS_TYPE="HypPart1" SUBS_CONTENT="nersen"/>
</TextLine>
</root>