Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Xpath根据其同级的文本和属性选择元素_C#_Xpath_Html Agility Pack - Fatal编程技术网

C# 使用Xpath根据其同级的文本和属性选择元素

C# 使用Xpath根据其同级的文本和属性选择元素,c#,xpath,html-agility-pack,C#,Xpath,Html Agility Pack,查看文档,目标是选择 第一个表中第二行的第二个单元格 我创建了以下表达式: //row/td[2]/text()[td[@class="identifier"]/span[text()="identifier"]] 但它不返回任何行。不幸的是,我看不出有什么问题 对我来说,看起来还不错。措辞应: select the text in the second cell in any row where the text of a span equals to "i

查看文档,目标是选择 第一个表中第二行的第二个单元格

我创建了以下表达式:

//row/td[2]/text()[td[@class="identifier"]/span[text()="identifier"]]
但它不返回任何行。不幸的是,我看不出有什么问题

对我来说,看起来还不错。措辞应:

select the text
    in the second cell 
        in any row
where
    the text of a span equals to "identifier"
        and the span is located in cell with a "identifier" class
如果你能指出我做错了什么,我将不胜感激

示例XML文档:

<?xml version="1.0"?>
<html>
    <table class="first">
        <tr>
            <td>row 1, cell 1</td>
            <td>row 1, cell 2</td>
        </tr>
        <tr>
            <td class="identifier">
                <span>identifier</span>
            </td>
            <td>
                foo
                <span>ignore</span>
                bar
            </td>
        </tr>
        <tr>
            <td>row 3, cell 1</td>
            <td>row 3, cell 2</td>
        </tr>
    </table>

    <table class="second">
        <tr>
            <td>row 1, cell 1</td>
            <td>row 1, cell 2</td>
        </tr>
        <tr>
            <td class="identifier">
                <span>not an identifier</span>
            </td>
            <td>
                not a target
            </td>
        </tr>
        <tr>
            <td>row 3, cell 1</td>
            <td>row 3, cell 2</td>
        </tr>
    </table>
</html>

第1行,第1单元
第1行第2单元
标识符
福
忽视
酒吧
第3行,第1单元
第3行第2单元
第1行,第1单元
第1行第2单元
不是标识符
不是目标
第3行,第1单元
第3行第2单元

乍一看,我想你的意思是:

//tr/td[2][@class="identifier"][span="identifier"]
奇怪的是,我在示例数据中没有看到任何节点与这三个条件都匹配

编辑:根据反馈,这里是另一个要尝试的查询

//tr[td/@class="identifier"][td/span="identifier"]/td[2]/text()

乍一看,我想你的意思是:

//tr/td[2][@class="identifier"][span="identifier"]
奇怪的是,我在示例数据中没有看到任何节点与这三个条件都匹配

编辑:根据反馈,这里是另一个要尝试的查询

//tr[td/@class="identifier"][td/span="identifier"]/td[2]/text()

不,我不这么认为。标记具有类似键/值的结构,但罪魁祸首是单元格的类名不是键,第一个单元格中跨度的内容是键。因此
@class
属性覆盖整行,即使它只在第一个单元格中?不,我不这么认为。标记具有类似键/值的结构,但罪魁祸首是单元格的类名不是键,第一个单元格中跨度的内容是键。因此
@class
属性覆盖了整行,即使它只在第一个单元格中?