说我有几个人物和他们写的书的XML文件,就像这样:
<person name="Margret Jane" id="mj">
<book>
<author id="mj"> Margret Jane </author>
<author id="js"> Sarah Jane </author>
<author id="ms"> Michael Scott </author>
</book>
<book>
<author id="mj"> Margret Jane </author>
<author id="fr"> Freya Rob </author>
<author id="ms"> Michael Scott </author>
</book>
等等
我将如何计算Margret和Michael是合著者的时间。
我已经使用过:
$getMargret = new DOMDocument();
$getMargret->load("Margret.xml");
$counter= $getMargret->getElementsByTagName("person")[0]->getElementsByTagName('book')->length;
print_r($counter);
为了获得Margret自己撰写的全部书籍,但我不确定如何计算共同作者的次数。
一个选项是使用DOMXpath检查两个名称:
/person/book[author = ' Margret Jane ' and author = ' Michael Scott ']
代码示例
$getMargret = new DOMDocument();
$getMargret->load("Margret.xml");
$xpath = new DOMXpath($getMargret);
$counter= $xpath->query("/person/book[author = ' Margret Jane ' and author = ' Michael Scott ']")->length;
echo $counter;
输出
2
参见Php demo