我想做的是计算根元素下的元素。然后检查同一级别的一个 id 是否具有 id 值。发生这种情况时,它需要增加一。
编码
public function _generate_id()
{
$id = 0;
$xpath = new DOMXPath($this->_dom);
do{
$id++;
} while($xpath->query("/*/*[@id=$id]"));
return $id;
}
示例 xml
<?xml version="1.0"?>
<catalog>
<book id="0">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
<book id="1">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.</description>
</book>
</catalog>