问题
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