getNodePath()不显示元素


getNodePath() not showing elements

我正在尝试使用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("//*[@Name]") ;  
foreach($domNodeList as $domNodeListItem)
{
    if(preg_match('/^rectangle.*/i',$domNodeListItem->getAttribute('Name')) === 1)
    {
        echo 'path: <pre>'.print_r($domNodeListItem->getNodePath(),true).'</pre>' ;
    }
}

XML示例(以下示例给出/*/*[1]/*/*[2]作为输出,期望是/Report/Rectangle/Report/MUAHAHA):

<?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>

找到答案了

返回的路径是由于默认命名空间。显然DOMDocuments和XPath都不喜欢这个XML特性。

我使用的解决方法是删除默认名称空间,获取路径,然后替换它。当我有时间的时候,我会添加一个例子。

给默认名称空间一个前缀是最好的,但是在我的例子中这是不可能的。