getNodePath()不显示元素

时间:2015-10-19 14:43:39

标签: 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("//*[@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>

1 个答案:

答案 0 :(得分:0)

找到答案。

返回的路径是由默认命名空间引起的。显然,DOMDocuments和XPath都不像这个XML特性。

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

首选赋予默认命名空间前缀,但在我的情况下,这是不可能的。