查找具有命名空间的 XML 节点的 XML::LibXML 问题 [英] XML::LibXML issue finding XML nodes with a namespace
问题描述
我正在编写 XML 解析器,但处理链接的程序有问题.我正在尝试解析 XML 层次结构设置/设置,然后查找节点值".以下是 XML 的示例:
I am writing an XML parser and am having an issue with the program handling a link. I am attempting to parse an XML hierarchy Settings/Setting then findnode 'Value'. The following is an example of the XML:
<?xml version='1.0' ?>
<Settings xmlns='http://hme.com/Settings.xsd'>
<Setting SID="0">
<Name>Store ID</Name>
<Value>72</Value>
</Setting>
<Setting SID="1">
<Name>Deprecated</Name>
<Value>0</Value>
</Setting>
<Setting SID="8">
<Name>Open Store Hours Sunday</Name>
<Value>25200</Value>
</Setting>
这是我用来解析 XML 的代码
Here is the code I am using to parse the XML
my $doc = $parser->parse_file($settings_file) or die "Couldn't parse timer settings
";
#Sunday
for my $reviewer ($doc->findnodes('/Settings/Setting[@SID="8"]')) {
my ($name) = $reviewer->findnodes('Value');
$name->removeChildNodes();
$name->appendText('109800');
}
当我从 XML 文件中删除 xmlns='http://hme.com/Settings.xsd'
时,替换值节点没有问题.一旦我在 XML 中输入链接,代码将停止工作并且不会更新 xml 表.有没有办法处理此链接或将其删除,以便我可以正确更新文件?
When I remove the xmlns='http://hme.com/Settings.xsd'
from the XML file, there is no issue with replacing the value node. Once I enter the link back in the XML, the code stops working and will not update the xml sheet. Is there a way to handle for this link or to remove it so I can properly update the file?
推荐答案
我能够使用这段代码来完成这个工作.
I was able to get this working using this code.
my $dom = XML::LibXML->load_xml(location => $filename);
my $xpc = XML::LibXML::XPathContext->new($dom);
$xpc->registerNs('xsd', 'http://hme.com/Settings.xsd');
my($match1) = $xpc->findnodes('//xsd:Settings/xsd:Setting[@SID="8"]/xsd:Value');
$match1->removeChildNodes();
$match1->appendText('23400');
这篇关于查找具有命名空间的 XML 节点的 XML::LibXML 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!