首页 > 解决方案 > SimpleXMLElement::xpath 不区分数字作为字符串

问题描述

我有带有如下标签的xml:

<tag id="02220">
<tag id="2220">

当我搜索例如:

Xml->xpath('//tag [@id='."2220".']');

结果将是两个 id 的数组。如何让 xpath 将它们视为字符串?

标签: phpxmlxpath

解决方案


You will need to include the quotes around the value you are looking for, currently your query comes out as

echo '//tag[@id='."2220".']';

gives

//tag[@id=2220]

as you can see, it is a number. Add quotes round the field...

echo '//tag[@id="'."2220".'"]';

will give you

//tag[@id="2220"]

推荐阅读