在 php 中通过 id 获取元素并通过 Value xpath 获取元素
get element by id and get element by Value xpath in php
XML 文件
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ew-language id="en">
<global>
<phrase id="actiondeleted" value="Deleted">
<child_phrase_1 id="1234" value="numbers">
<child id="test_id" value="test_value"/>
<child id="test_id" value="test_value_2"/>
</child_phrase_1>
</phrase>
</global>
</ew-language>
如何通过 ID 和值获取元素以便元素是 unique.I 尝试了这些
$parent = ($xpath->query("//*[@id='$previous_tag_id']")&& $xpath->query("//*[@value='$previous_tag_value']"))->item(0);
----------------------AND THIS ONE-----------------------------------
$parent = $xpath->query("//*[@id='$previous_tag_id']/*@value='$previous_tag_value')"->item(0);
----------------------AND THIS ONE-----------------------------------
$xpath->query("//*[@id='$previous_tag_id' and @value='$previous_tag_value']");
每个语法都不起作用。
使用 //*[@id='$previous_tag_id' and @value='$previous_tag_value']
应该有效,请参阅 eval.in 中的演示:
$xml = <<<XML
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ew-language id="en">
<global>
<phrase id="actiondeleted" value="Deleted">
<child_phrase_1 id="1234" value="numbers">
<child id="test_id" value="test_value"/>
<child id="test_id" value="test_value_2"/>
</child_phrase_1>
</phrase>
</global>
</ew-language>
XML;
$doc = new DOMDocument;
$doc->loadXML($xml);
$xpath = new DOMXPath($doc);
$previous_tag_id = "test_id";
$previous_tag_value = "test_value_2";
$result = $xpath->query("//*[@id='$previous_tag_id' and @value='$previous_tag_value']");
foreach($result as $r)
{
echo $doc->saveXML($r);
}
输出:
<child id="test_id" value="test_value_2"/>
XML 文件
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ew-language id="en">
<global>
<phrase id="actiondeleted" value="Deleted">
<child_phrase_1 id="1234" value="numbers">
<child id="test_id" value="test_value"/>
<child id="test_id" value="test_value_2"/>
</child_phrase_1>
</phrase>
</global>
</ew-language>
如何通过 ID 和值获取元素以便元素是 unique.I 尝试了这些
$parent = ($xpath->query("//*[@id='$previous_tag_id']")&& $xpath->query("//*[@value='$previous_tag_value']"))->item(0);
----------------------AND THIS ONE-----------------------------------
$parent = $xpath->query("//*[@id='$previous_tag_id']/*@value='$previous_tag_value')"->item(0);
----------------------AND THIS ONE-----------------------------------
$xpath->query("//*[@id='$previous_tag_id' and @value='$previous_tag_value']");
每个语法都不起作用。
使用 //*[@id='$previous_tag_id' and @value='$previous_tag_value']
应该有效,请参阅 eval.in 中的演示:
$xml = <<<XML
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ew-language id="en">
<global>
<phrase id="actiondeleted" value="Deleted">
<child_phrase_1 id="1234" value="numbers">
<child id="test_id" value="test_value"/>
<child id="test_id" value="test_value_2"/>
</child_phrase_1>
</phrase>
</global>
</ew-language>
XML;
$doc = new DOMDocument;
$doc->loadXML($xml);
$xpath = new DOMXPath($doc);
$previous_tag_id = "test_id";
$previous_tag_value = "test_value_2";
$result = $xpath->query("//*[@id='$previous_tag_id' and @value='$previous_tag_value']");
foreach($result as $r)
{
echo $doc->saveXML($r);
}
输出:
<child id="test_id" value="test_value_2"/>