How do i make Xpath 1.0 query case insensitive

无人久伴 提交于 2020-01-17 11:15:47

问题


In PHP, I'm currently making a xpath query but I need to make it case insensitive. I'm using is XPath 1.0 which from my query means I've got to use some thing called a translate function but I'm unsure of how to do this.

Here is my query test PHP file :

$html = <<<'HTML'
<html>
<head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8">
    <meta NAME="Description" content="Test Case">
    <META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
    <Link Rel="Canonical" href="http://www.testsite.com/" />
    <Title>My Title</Title>
</head>
<Body>
Test  Case
</Body>
</html>
HTML;

$domDoc = new DOMDocument();
$domDoc->loadHTML('<?xml encoding="utf-8" ?>' . $html);

// Canonical link
$xpath = new DOMXPath($domDoc);
$canonicalTags = $xpath->query('//link[@rel=\'canonical\']'); // Return nothing
//some use translate(WhatVariable?, 'ABCDEFGHIJKLMNOPQRSTUVWXYZÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞŸŽŠŒ', 'abcdefghijklmnopqrstuvwxyzàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿžšœ')

var_dump($canonicalTags);

Any help would be greatly appreciated. Thanks.


回答1:


Basically, translate is used to convert dynamic value that you need to compare to be all lower-case (or all upper-case). In this case, you want to apply translate() to rel attribute value, and compare the result to lower-case literal "canonical" (formatted for readability) :

//link[
    translate(@rel, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = 'canonical'
]


来源:https://stackoverflow.com/questions/41394579/how-do-i-make-xpath-1-0-query-case-insensitive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!