Php 如何在第一个元素xpath中找到元素的出现位置
使用如下XML结构:Php 如何在第一个元素xpath中找到元素的出现位置,php,xpath,Php,Xpath,使用如下XML结构: <w:document> <w:body> <w:p>{textcontent}</w:p> <w:p> <w:drawing> </w:drawing> </w:p> <w:p>{no textcontent}</w:p> <w:p>
<w:document>
<w:body>
<w:p>{textcontent}</w:p>
<w:p>
<w:drawing>
</w:drawing>
</w:p>
<w:p>{no textcontent}</w:p>
<w:p>
<w:drawing>
</w:drawing>
</w:p>
<w:p>{no textcontent}</w:p>
<w:p>text..</w:p>
<w:tbl>three</w:tbl>
<w:p>four</w:p>
</w:body>
</w:document>
但是我还需要p的索引($pindex
)
我还尝试了evaluate('/child')
,evaluate('node()/child')
,但没有成功
我的表达式出了什么问题?严格从xpath的角度来看: 添加
<root xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'>
[your xml above]
</root>
给出了输出:
2.0
如果需要整数而不是浮点,请将表达式更改为
round(count(//w:p/w:drawing))
希望这对其他方面有所帮助。严格从xpath的角度来看: 添加
<root xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'>
[your xml above]
</root>
给出了输出:
2.0
如果需要整数而不是浮点,请将表达式更改为
round(count(//w:p/w:drawing))
希望这对其他人有帮助。我实际上找到了我自己问题的答案:-) 而不是:
$document = new DOMDocument();
$document->loadXML($xml_str);
$xpath = new DOMXpath($document);
$xpath->registerNamespace(
'wspace', 'http://schemas.openxmlformats.org/wordprocessingml/2006/main'
);
$pindex = 0;
foreach ($xpath->evaluate('//wspace:p') as $index => $p_node)
{
if (strlen($p_node->textContent)>0) {
//I just want to know if drawing exists in this $p_node..
//if the the node contains any content
$child_nodes = $xpath->evaluate('//wspace:drawing', $p_node);
var_dump($child_nodes[0]);
$pindex++;
//But I get an object for each pnode. I just want an object when there
//is an actualy <w:drawing> inside of <p> (item with index 1,3 in above example)
}
}
$child_nodes = $xpath->evaluate('//wspace:drawing', $p_node);
$child_nodes = $p_node->childNodes;
foreach($child_nodes as $rc)
{
if ($rc->localName == 'drawing')
{
$img_count++;
echo 'P=' . $pindex;
}
}
我只是在当前$p_节点上使用childNodes:
$document = new DOMDocument();
$document->loadXML($xml_str);
$xpath = new DOMXpath($document);
$xpath->registerNamespace(
'wspace', 'http://schemas.openxmlformats.org/wordprocessingml/2006/main'
);
$pindex = 0;
foreach ($xpath->evaluate('//wspace:p') as $index => $p_node)
{
if (strlen($p_node->textContent)>0) {
//I just want to know if drawing exists in this $p_node..
//if the the node contains any content
$child_nodes = $xpath->evaluate('//wspace:drawing', $p_node);
var_dump($child_nodes[0]);
$pindex++;
//But I get an object for each pnode. I just want an object when there
//is an actualy <w:drawing> inside of <p> (item with index 1,3 in above example)
}
}
$child_nodes = $xpath->evaluate('//wspace:drawing', $p_node);
$child_nodes = $p_node->childNodes;
foreach($child_nodes as $rc)
{
if ($rc->localName == 'drawing')
{
$img_count++;
echo 'P=' . $pindex;
}
}
事实上,我找到了我自己问题的答案:-) 而不是:
$document = new DOMDocument();
$document->loadXML($xml_str);
$xpath = new DOMXpath($document);
$xpath->registerNamespace(
'wspace', 'http://schemas.openxmlformats.org/wordprocessingml/2006/main'
);
$pindex = 0;
foreach ($xpath->evaluate('//wspace:p') as $index => $p_node)
{
if (strlen($p_node->textContent)>0) {
//I just want to know if drawing exists in this $p_node..
//if the the node contains any content
$child_nodes = $xpath->evaluate('//wspace:drawing', $p_node);
var_dump($child_nodes[0]);
$pindex++;
//But I get an object for each pnode. I just want an object when there
//is an actualy <w:drawing> inside of <p> (item with index 1,3 in above example)
}
}
$child_nodes = $xpath->evaluate('//wspace:drawing', $p_node);
$child_nodes = $p_node->childNodes;
foreach($child_nodes as $rc)
{
if ($rc->localName == 'drawing')
{
$img_count++;
echo 'P=' . $pindex;
}
}
我只是在当前$p_节点上使用childNodes:
$document = new DOMDocument();
$document->loadXML($xml_str);
$xpath = new DOMXpath($document);
$xpath->registerNamespace(
'wspace', 'http://schemas.openxmlformats.org/wordprocessingml/2006/main'
);
$pindex = 0;
foreach ($xpath->evaluate('//wspace:p') as $index => $p_node)
{
if (strlen($p_node->textContent)>0) {
//I just want to know if drawing exists in this $p_node..
//if the the node contains any content
$child_nodes = $xpath->evaluate('//wspace:drawing', $p_node);
var_dump($child_nodes[0]);
$pindex++;
//But I get an object for each pnode. I just want an object when there
//is an actualy <w:drawing> inside of <p> (item with index 1,3 in above example)
}
}
$child_nodes = $xpath->evaluate('//wspace:drawing', $p_node);
$child_nodes = $p_node->childNodes;
foreach($child_nodes as $rc)
{
if ($rc->localName == 'drawing')
{
$img_count++;
echo 'P=' . $pindex;
}
}
我不只是想数一数发生的事情。我还需要的与获取相关的索引。我不想只计算发生次数。我还需要的与获取相关的索引。