dqluw20882
2014-12-15 16:03
浏览 44

xpath-> query()仅适用于星号

Below is the code I'm currently working with.

The input XML file is available here: http://pastebin.com/hcQhPSjs

header("Content-Type: text/plain");
  $xmlFile = new domdocument();
  $xmlFile->preserveWhiteSpace = false;
  $xmlFile->load("file:///srv/http/nginx/html/xml/UNSD_Quest_Sample.xml");
  $xpath = new domxpath($xmlFile);
  $hier = '//Workbook';
  $result = $xpath->query($hier);
  foreach ($result as $element) {
    print $element->nodeValue;
    print "
";
  };

Now for the $hier variable, PHP won't parse the results unless I use the wildcard * to reach the nodes I need. So instead of using the usual /Workbook/Worksheet/Table/Row/Cell/Data method of accessing nodes, I'm relegated to /*/*[6]/*[2]/* The input file is an excel spreadsheet exported to xml. Seems like the issue might be in the export from xls to xml.

What I find peculiar is the fact that Firefox (default browser) does not parse the namespace attributes for the root element <Workbook> while Chromium and/or any text editor do.
Firefox:

<?mso-application progid="Excel.Sheet"?>
<Workbook>
<DocumentProperties>
<Author>Htike Htike Kyaw Soe</Author>
<Created>2014-01-14T20:37:41Z</Created>
<LastSaved>2014-12-04T10:05:11Z</LastSaved>
<Version>14.00</Version>
</DocumentProperties>
<OfficeDocumentSettings>
<AllowPNG/>
</OfficeDocumentSettings>

Chromium:

<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40">
<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
<Author>Htike Htike Kyaw Soe</Author>
<Created>2014-01-14T20:37:41Z</Created>
<LastSaved>2014-12-04T10:05:11Z</LastSaved>
<Version>14.00</Version>
</DocumentProperties>
<OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office">
<AllowPNG/>
</OfficeDocumentSettings>  

Could anyone explain why this is the case?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duangou1933 2014-12-15 16:18
    已采纳

    You need to register and use a namespace prefix for the namespace used in the XML. From the tag and element names I expect it to be urn:schemas-microsoft-com:office:spreadsheet - Excel Spreadsheet. So here is an example for that:

    $xml = <<<'XML'
    <?xml version="1.0"?>
    <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet">
      <Worksheet>
        <Table>
          <Row>
            <Cell>
              <Data>TEST</Data>
            </Cell>
          </Row>
        </Table>
      </Worksheet>
    </Workbook>
    XML;
    
    $dom = new DOMDocument();
    $dom->preserveWhiteSpace = false;
    $dom->loadXML($xml);
    $xpath = new DOMXpath($dom);
    $xpath->registerNamespace('s', 'urn:schemas-microsoft-com:office:spreadsheet');
    
    $expression = '/s:Workbook/s:Worksheet/s:Table/s:Row/s:Cell/s:Data';
    $result = $xpath->evaluate($expression);
    foreach ($result as $element) {
      print $element->nodeValue;
      print "
    ";
    }
    

    Output:

    TEST
    

    You should not use DOMXpath::query() but DOMXpath::evaluate(). It allows you to fetch scalar values using XPath, too.

    $expression = 'string(/s:Workbook/s:Worksheet/s:Table/s:Row/s:Cell/s:Data)';
    echo $xpath->evaluate($expression);
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题