【问题标题】:XML::LibXML parsing and choosing element a quick oneXML::LibXML 快速解析和选择元素
【发布时间】:2011-11-30 10:08:01
【问题描述】:

在编程方面,我总是遇到一些逻辑问题。我认为这是合乎逻辑且容易的,但对我来说很难,因为我无法到达那里。我正在使用 XML::LibXML 来解析 XML 文件。现在在下面的代码中

<CommentsCorrectionsList>
<CommentsCorrections RefType="Cites">
<RefSource>Brain Cogn. 2005 Jul;58(2):245</RefSource>
</CommentsCorrections>
<CommentsCorrections RefType="RepublishedIn">
<RefSource>Brain Cogn. 2005 Jul;58(2):246-8</RefSource>
<PMID Version="1">16044513</PMID>
</CommentsCorrections>
<CommentsCorrections RefType="PartialRetractionOf">
<RefSource>Curr Opin Organ Transplant. 2001 Mar;6(1):95-101</RefSource>
</CommentsCorrections>
</CommentsCorrectionsList>

我想为除“Cites”之外的所有其他 RefType 选择 cmetscorrections。我该怎么做。我想通过将所有想要的 RefTypes 放在另一个变量中,然后使用它来获取其他数据来做到这一点。这是正确的方法吗,我尝试了一些虚拟变量,如下所示

my $sam = "A" || "B" || "C";

print "test= ";
my $test = <>;
if ($test == $sam) {
print $test;
print "success";} else {
print "NO";}

我知道这对你们中的一些人来说可能很愚蠢,但我在一个月左右的时间里编写了一个程序,有时我会因为不知道该怎么做而感到沮丧。我尝试学习很多东西。如果这真的是一个愚蠢的问题,请原谅我。

还有,我想这样做

if(!($foo->findnodes('CommentsCorrectionList/CommentsCorrections[@RefType="Cites"]'))){
do foreach and get the data
}

但在这种情况下,我如何避免 foreach 中的 RefType="Cites" 并使其等于我想要的另一个 RefType。我的意思是我不知道这种布尔值是否可以在 foreach 语句中使用。我试着寻找,也做了试验和错误,但手头没有。非常感谢任何帮助。

谢谢。

【问题讨论】:

    标签: perl xpath xml-parsing xml-libxml


    【解决方案1】:
    CommentsCorrectionList/CommentsCorrections[@RefType != "Cites"]
    

    我个人使用XPath spec 作为我的参考,但那里可能有更友好的参考。

    【讨论】:

    • 是的..这就是我的想法。谢谢你的参考,真的很有帮助。谢谢你的ikegami。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多