dongliao1949 2017-08-02 14:45
浏览 142
已采纳

使用带有联合的xPath选择节点

I'm writing a basic HTML report templating system based on manipulating the data using xPath. Essentially, I need an xPath query which will select a node OR its children if they have a certain class.

$query = $xPath->query (".//*[contains(concat(' ', normalize-space(@class), ' '), ' -delete-if-no-stock ')]", $node);

I understand that the xPath selector .//*[contains(concat(' ', normalize-space(@class), ' '), ' -delete-if-no-stock ')] is specifically looking at descendant nodes of the $node parameter.

I would like an xPath query that essentially asks "the node or any of its children". I know that there is a union operator, |, but I haven't seen how to implement that. I would have imagined something like this: .[contains(concat(' ', normalize-space(@class), ' '), ' -delete-if-no-stock ')] | .//*[contains(concat(' ', normalize-space(@class), ' '), ' -delete-if-no-stock ')] but this generates an Invalid expression error.

  • 写回答

2条回答 默认 最新

  • doucheng1884 2017-08-10 08:04
    关注

    Xpath expressions work as filters. They do not aggregate/compile in that kind of sense (Like an SQL Union).

    Here are several possibilities depending on what you're trying to do.

    Alternative Expressions

    The pipe symbol | allows you to specify multiple expressions - it works like the comma , in CSS selectors.

    expression_one|expression_two

    Complex conditions

    Conditions in the expressions can use and and or as well as brackets.

    /location/path[condition and condition or condition]

    Axis

    Xpath expressions have a concept of axis that define the initial set of nodes the filter is applied to.

    axis::node[condition]

    .//* is short for self::node()/descendant::*. Here is a axis called descendant-or-self that includes the current node and all descendants.

    descendant-or-self::*[contains(...)]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常