Php getNodePath()不显示元素
我正在尝试使用DOMDocuments获取节点的XPath。然而,我得到的回应是Php getNodePath()不显示元素,php,xml,xpath,domdocument,domxpath,Php,Xml,Xpath,Domdocument,Domxpath,我正在尝试使用DOMDocuments获取节点的XPath。然而,我得到的回应是/*/*[2]/*/*[1]/*[1]/*[5]。有人知道这是为什么和/或可能的解决方案吗 我的代码: $doc = new DOMDocument(); $doc->loadXML(file_get_contents($_FILES["xmlfile"]["tmp_name"])); $xp = new DOMXpath($doc); $domNodeList = $xp->evaluate("//
/*/*[2]/*/*[1]/*[1]/*[5]
。有人知道这是为什么和/或可能的解决方案吗
我的代码:
$doc = new DOMDocument();
$doc->loadXML(file_get_contents($_FILES["xmlfile"]["tmp_name"]));
$xp = new DOMXpath($doc);
$domNodeList = $xp->evaluate("//*[@Name]") ;
foreach($domNodeList as $domNodeListItem)
{
if(preg_match('/^rectangle.*/i',$domNodeListItem->getAttribute('Name')) === 1)
{
echo 'path: <pre>'.print_r($domNodeListItem->getNodePath(),true).'</pre>' ;
}
}
找到了答案
返回的路径是由于默认命名空间引起的。显然,DOMDocuments和XPath都不像这个XML特性
我使用的解决方法是删除默认名称空间,获取路径,然后替换它。当我有时间时,我将添加一个示例
最好给默认名称空间一个前缀,但这在我的情况下是不可能的。你希望得到什么?我希望路径包含元素名称,而不是通配符。你能提供一个小示例XML,以便我们重现问题吗?在我的原始帖子中添加了一部分我正在使用的测试XML。
<?xml version="1.0" encoding="UTF-8"?>
<Report xmlns="http://schemas.microsoft.com/sqlserver/reporting/2010/01/reportdefinition" xmlns:cl="http://schemas.microsoft.com/sqlserver/reporting/2010/01/componentdefinition" xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportdesigner">
<Rectangle Name="Rectangle3">
<ReportItems>
<Textbox Name="Textbox3">
<CanGrow>true</CanGrow>
<KeepTogether>true</KeepTogether>
<Paragraphs>
<Paragraph>
<TextRuns>
<TextRun>
<Value />
<Style />
</TextRun>
</TextRuns>
<Style />
</Paragraph>
</Paragraphs>
<rd:DefaultName>Textbox3</rd:DefaultName>
<Top>0.16667in</Top>
<Left>0.15625in</Left>
<Height>0.25in</Height>
<Width>1in</Width>
<Style>
<Border>
<Style>None</Style>
</Border>
<PaddingLeft>2pt</PaddingLeft>
<PaddingRight>2pt</PaddingRight>
<PaddingTop>2pt</PaddingTop>
<PaddingBottom>2pt</PaddingBottom>
</Style>
</Textbox>
</ReportItems>
<KeepTogether>true</KeepTogether>
<Top>0.66542in</Top>
<Left>2.53in</Left>
<Height>2.60417in</Height>
<Width>1.47917in</Width>
<ZIndex>3</ZIndex>
<Style>
<Border>
<Style>None</Style>
</Border>
</Style>
</Rectangle>
<MUAHAHA Name="Rectangle123">
<ReportItems>
<Textbox Name="Textbox3">
<CanGrow>true</CanGrow>
<KeepTogether>true</KeepTogether>
<Paragraphs>
<Paragraph>
<TextRuns>
<TextRun>
<Value />
<Style />
</TextRun>
</TextRuns>
<Style />
</Paragraph>
</Paragraphs>
<rd:DefaultName>Textbox3</rd:DefaultName>
<Top>0.16667in</Top>
<Left>0.15625in</Left>
<Height>0.25in</Height>
<Width>1in</Width>
<Style>
<Border>
<Style>None</Style>
</Border>
<PaddingLeft>2pt</PaddingLeft>
<PaddingRight>2pt</PaddingRight>
<PaddingTop>2pt</PaddingTop>
<PaddingBottom>2pt</PaddingBottom>
</Style>
</Textbox>
</ReportItems>
<KeepTogether>true</KeepTogether>
<Top>0.66542in</Top>
<Left>2.53in</Left>
<Height>2.60417in</Height>
<Width>1.47917in</Width>
<ZIndex>3</ZIndex>
<Style>
<Border>
<Style>None</Style>
</Border>
</Style>
</MUAHAHA>
</Report>