【问题标题】:Why is this XPath expression invalid?为什么这个 XPath 表达式无效?
【发布时间】:2020-04-21 21:19:40
【问题描述】:

谁能告诉我为什么这个 XPath 表达式无效?

/ReaderDetails/Reader[Pin=('2325', '2421', '2424')]

完整代码:

$dream_team_pins = implode('\', \'', array_map('trim',explode(',',get_option('dream_team_readers'))));
// Takes a comma separated list, converts to an array, and then implodes to a quoted comma separated list.
// This is currently outputting 2325', '2421', '2424 (the missing quotes at start and end are intentional)

$expression = '/ReaderDetails/Reader[Pin=(\''.$dream_team_pins.'\')]';
// I'm adding the single quotes at the start and end back in here so the entire expression is 
// outputting /ReaderDetails/Reader[Pin=('2325', '2421', '2424')]

$dream_team_readers = $readers->xpath($expression);

但我不断得到

警告:SimpleXMLElement::xpath():无效的表达式。

简而言之,我希望能够将多个值传递给一个 xpath 表达式(取自逗号分隔的字符串)来过滤 XML 数组。

对我的表达有什么建议吗?

【问题讨论】:

    标签: xml xpath xpath-2.0 xpath-1.0


    【解决方案1】:

    该 XPath 表达式使用 XPath 2.0 构造,但您的 XPath 库仅实现 XPath 1.0。

    改变

    [Pin=('2325', '2421', '2424')]
    

    [Pin='2325' or Pin='2421' or Pin='2424')]
    

    在 XPath 1.0 中实现析取。

    【讨论】:

    • 根据详细的逗号分隔列表循环输出的最佳 PHP 函数是什么?
    • 如果您需要 PHP 帮助来调整您的 array_map 调用,请使用 php 标签将目标和您尝试的内容作为新问题发布,以吸引 PHP 开发人员的注意。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2015-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-12
    相关资源
    最近更新 更多