我的XML:
<root xmlns="mynamespace">
<measInfo measInfoId="CORE,SIP_session_statistics">
<measType>CPUUSAGE</measType>
<measType>CPUMEM</measType>
<measType>SYSMEM</measType>
<measValue measObjLdn='bsNo=18'>
<r>1</r>
<r>2</r>
<r>3</r>
</measValue>
<measValue measObjLdn='bsNo=19'>
<r>4</r>
<r>5</r>
<r>6</r>
</measValue>
</measInfo>
<measInfo measInfoId="CORE,SIP_session_statistics">
<measType>CPUUSAGE</measType>
<measType>CPUMEM</measType>
<measType>SYSMEM</measType>
<measValue measObjLdn='bsNo=17'>
<r>1</r>
<r>2</r>
<r>3</r>
</measValue>
</measInfo>
</root>
显然,我的要求与上一个问题类似,但这次它保持变量和命名空间问题,正如我们所知。
我想转到特定<r>
<measType>CPUUSAGE</measType>
元素
我必须在变量中存储一些东西:
my $ptag=measInfo;
my $ktag=measType;
my $vtag=measValue;
my $whr="CPUUSAGE";
my $mn="measObjLdn";
my $rng='bsNo=18';
我正在使用$pos
找到<r>
元素的正确顺序。
for my $type_node ($conn->findnodes("//\*[name()='$ptag'][\*[name()='$ktag'][.='$whr']]")) {
$pos = $type_node->findvalue("count(preceding-sibling::$ktag) + 1");
$qry = <<"EOF";
..
/$mv[contains(concat(\@$ml, ','), '$rng')]
/r[$pos]
EOF
for my $r_node ($type_node->findnodes($qry)) {
$r_node->removeChildNodes;
$r_node->appendText($new_value);
}
}
Q&GT;什么都行不通。可能是愚蠢的错误????
答案 0 :(得分:0)
不确定这是否正是您想要的,但命名空间用法和xpath可能会有所帮助。
my $parser = XML::LibXML->new();
my $document = $parser->load_xml(location => $input_file);
my $xml_root = $document->documentElement();
my $xmlns_root = XML::LibXML::XPathContext->new($xml_root);
$xmlns_root->registerNs('mynamespace', 'mynamespace');
foreach my $type_node ($xmlns_root->findnodes('/mynamespace:root/mynamespace:measInfo[mynamespace:measType/text()="CPUUSAGE"]/mynamespace:measValue[@measObjLdn="bsNo=18"]/mynamespace:r[1]')) {
print $type_node->textContent();
}
答案 1 :(得分:0)
部分解决:现在可以使用变量。
PENDING: NameSpace问题悬而未决。
for my $type_node ($conn->findnodes("//${ptag}/${ktag}[.='$whr']")) {
$pos = $type_node->findvalue("count(preceding-sibling::$ktag) + 1");
$qry = "../${vtag}[\@$atag='$rng']/r[$pos]";
for my $r_node ($type_node->findnodes($qry)) {
$r_node->removeChildNodes;
$r_node->appendText($new_value);
}
}