Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
308 views
in Technique[技术] by (71.8m points)

php - How do i make Xpath 1.0 query case insensitive

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àá??????èéê?ìí??D?òó????ùú?üYT????', 'abcdefghijklmnopqrstuvwxyzàáa?????èéê?ìí??e?òó????ùú?üyt????')

var_dump($canonicalTags);

Any help would be greatly appreciated. Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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'
]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...