oli*_*erg 2 xml perl loops libxml2 xml-libxml
我在使用 XML::LibXML 时遇到一些问题,我想知道是否有办法做我想做的事情,或者是否应该更改我的 XML。
目前,我的 XML 如下所示:
<fftg>
<actions>
<rename>
<mandatory>0</mandatory>
<other_things_here />
</rename>
<transfer>
<mandatory>0</mandatory>
<protocol>SFTP</protocol>
<other_things_here />
</transfer>
<transfer>
<mandatory>1</mandatory>
<protocol>FTP</protocol>
<other_things_here />
</transfer>
<archive>
<mandatory>1</mandatory>
<other_things_here />
</archive>
<rename>
<mandatory>1</mandatory>
<other_things_here />
</rename>
</actions>
</fftg>
Run Code Online (Sandbox Code Playgroud)
如您所见,在“actions”下,可以有不同类型的操作(每种操作有 1 个或多个操作,每个操作下有不同的内容)
我想浏览每个操作并根据该操作执行特定的操作。
我的问题是:由于存在多个同类操作,因此脚本无法正常工作并覆盖前一个同类操作,或者特定操作上的循环会在同类每个操作上重新循环
示例1:
foreach my $transfer ($doc->findnodes('/fftg')) {
# Browse Actions
foreach my $action ($doc->findnodes('/fftg/actions/*')) {
my $action_name = $action->nodeName();
my $actiontype = ucfirst($action->nodeName());
print "executing action $action_name ..\n";
# Browse action details
foreach my $action_details ($doc->findnodes('/fftg/actions/'.$action_name)) {
for my $detail ($action_details->findnodes('./*')) {
print $detail->nodeName(), ": ", $detail->textContent(), "\n";
}
}
print "-------------\n";
}
}
Run Code Online (Sandbox Code Playgroud)
结果1:
executing action rename ..
mandatory: 0
other_things_here:
mandatory: 1
other_things_here:
-------------
executing action transfer ..
mandatory: 0
protocol: SFTP
other_things_here:
mandatory: 1
protocol: FTP
other_things_here:
-------------
executing action transfer ..
mandatory: 0
protocol: SFTP
other_things_here:
mandatory: 1
protocol: FTP
other_things_here:
-------------
executing action archive ..
mandatory: 1
other_things_here:
-------------
executing action rename ..
mandatory: 0
other_things_here:
mandatory: 1
other_things_here:
-------------
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,这个结果是错误的,因为在每个“动作类型”(这是好的,检测到的)中,它在同类的每个动作上重新循环。
这是因为 :
foreach my $action_details ($doc->findnodes('/fftg/actions/'.$action_name)) {
Run Code Online (Sandbox Code Playgroud)
我怎样才能达到我想要的目标?(如果可能的话,无需更改 XML)
我想要的是:
executing action rename ..
mandatory: 0
other_things_here:
-------------
executing action transfer ..
mandatory: 0
protocol: SFTP
other_things_here:
-------------
executing action transfer ..
mandatory: 1
protocol: FTP
other_things_here:
-------------
executing action archive ..
mandatory: 1
other_things_here:
-------------
executing action rename ..
mandatory: 1
other_things_here:
-------------
Run Code Online (Sandbox Code Playgroud)
谢谢
顺便说一句,我想我可以更改我的 XML 并使用类似的东西来轻松修复它(但我不知道哪一个是三者之间的最佳选择),但我想避免下面这两个因为 XSD 之后将很难生成(我想要每种操作类型的特定选项):
<fftg>
<actions>
<action>
<type>rename</type>
<mandatory>0</mandatory>
<other_things_here />
</action>
<action>
<type>transfer</type>
<mandatory>0</mandatory>
<protocol>SFTP</protocol>
<other_things_here />
<action>
<action>
<type>transfer</type>
<mandatory>0</mandatory>
<protocol>FTP</protocol>
<other_things_here />
<action>
<action>
<type>archive</type>
<mandatory>0</mandatory>
<other_things_here />
<action>
<action>
<type>rename</type>
<mandatory>0</mandatory>
<other_things_here />
<action>
</actions>
</fftg>
Run Code Online (Sandbox Code Playgroud)
或者
<fftg>
<actions>
<action type="rename">
<mandatory>0</mandatory>
<other_things_here />
</action>
<action type="transfer">
<mandatory>0</mandatory>
<protocol>SFTP</protocol>
<other_things_here />
<action>
<action type="transfer">
<mandatory>0</mandatory>
<protocol>FTP</protocol>
<other_things_here />
<action>
<action type="archive">
<mandatory>0</mandatory>
<other_things_here />
<action>
<action type="rename">
<mandatory>0</mandatory>
<other_things_here />
<action>
</actions>
</fftg>
Run Code Online (Sandbox Code Playgroud)
再次感谢问候,
首先,您调用findnodes on $doc,这意味着您的 XPath 表达式将根据文档根节点进行计算。其次,您使用的 XPath 表达式以“/”开头,以便再次以顶级节点为根。这样,当您致电时:
$doc->findnodes('/fftg/actions/rename')
Run Code Online (Sandbox Code Playgroud)
它的意思是“给我作为文档根节点<rename/>的元素的子<actions/>元素的子<fftg/>元素的每个元素”,因此,您将获得<rename/>在文档中找到的每个节点。
而且,循环太多。第一个是多余的,因为可能只有单个根元素。两个循环应该有效(一个针对<action/>子级,另一个针对子级):
for my $action ($doc->findnodes('/fftg/actions/*')) {
print "executing ", $action->nodeName(), "\n";
for my $detail ($action->findnodes('./*')) {
print $detail->nodeName(), ": ", $detail->textContent(), "\n";
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
126 次 |
| 最近记录: |