English 中文(简体)
Xpath expression to access parent or sibling using Rainbow:TreeBuilder:XPath model
原标题:Xpath expression to access parent or sibling using HTML::TreeBuilder::XPath module

我想用超文本选择母子或母子:TreeBuilder:XPath Perl模块, 让我们抽取如下的超文本:

<tbody>
    <tr>
        <td class="c1">Match_Text</td>
        <td class="c2">Extact_Text</td>
    </tr>
    <tr>
        <td class="c1"></td>
        <td class="c2"></td>
    </tr>
</tbody>

因此,我要为我作如下文 to:

 /html/body//td[@class="c1"]="Match_Text"/../td[@class="c2"] 

which is a valid expression for my use case.

但是,我想到的是母子没有执行,我从垂直模块中看到错误:

axis axis_parent not implemented [Can t locate object method "getParentNode" via package "XML::XPathEngine::Literal" at /usr/local/share/perl/5.10.1/XML/XPathEngine/Step.pm line 326. ]

任何人都可以提出一种可供本人使用的替代Xpath表述(接触母子/ no子)。 请注意,我只想用Xpath的表述来这样做,并有意制造一种多功能,并明确tra树。

最佳回答

The error message is misleading. The problem isn t lack of support, it s that you re trying to find the parent of the boolean returned by the comparison. It doesn t have one.

你可以使用

//*[ td[@class="c1" and text()="Match_Text"] ]/td[@class="c2"]

//td[@class="c1" and text()="Match_Text"]/following-sibling::*

//td[@class="c1" and text()="Match_Text"]/following-sibling::td[@class="c2"]

//td[@class="c1" and text()="Match_Text"]/../td[@class="c2"]
问题回答

我没有看到像以前那样的任何工作表现。 实际上,我认为,这一错误是由于这一表述没有很好地组成和划一。

Anyway, here are two alternatives for your goal – though not tested with the specified Perl module:

//td[@class="c1" and text()="Match_Text"]/../td[@class="c2"]
//td[@class="c2" and ../td[@class="c1" and text()="Match_Text"]]

http://www.shell-tools.net/“rel=“nofollow”>shell-tools和

I want to select parent or sibling of a node using HTML::TreeBuilder::XPath Perl module.

不管怎样,如果你想用XPath的话来选择几个节点,而且你知道每个节点都有单独的XPath表述,你就可以简单地使用标准XPath工会操作员>>>>。

 (/*/body//*[td[@class="c1"and . =  Match_Text ]])[1]
|
  (/*/body//*[td[@class="c1"and . =  Match_Text ]])[1]
               /td[[@class="c1"and not(. =  Match_Text )]

在《XPath》的表述中,/code>的操作员的第一演奏是参考节点的母体成分,工会经营者的第二项论点是选择该母子的所有子女内容的表述,其体格价值不同于参考节点。

结果是选择了两条 no子的结合。





相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...