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条)

报告相同问题?

悬赏问题

  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?