我有这样的xml:
<?xml version="1.0"?>
<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">
<Worksheet ss:Name="Name1">
something
</Worksheet>
<Worksheet ss:Name="Name2">
something else
</Worksheet>
</Workbook>
查询的外观应该如何使我变成具有 ss:Name 属性 Name1 的 Worksheet 元素。由于默认命名空间,我必须像这样设置第一个条件:
//*[name()="Worksheet"]
但我不知道如何添加属性条件...
-------更新-------因为我在这里找不到解决方案是所有xml文件(由excel生成的文件):
<?xml version="1.0"?>
<?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>Don diego</Author>
<LastAuthor>Don diego</LastAuthor>
<Created>2013-04-18T07:20:33Z</Created>
<LastSaved>2013-04-18T07:20:33Z</LastSaved>
<Company>CEI</Company>
<Version>14</Version>
</DocumentProperties>
<OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office">
<AllowPNG/>
</OfficeDocumentSettings>
<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
<WindowHeight>7740</WindowHeight>
<WindowWidth>13395</WindowWidth>
<WindowTopX>360</WindowTopX>
<WindowTopY>30</WindowTopY>
<ProtectStructure>False</ProtectStructure>
<ProtectWindows>False</ProtectWindows>
</ExcelWorkbook>
<Styles>
<Style ss:ID="Default" ss:Name="Normal">
<Alignment ss:Vertical="Bottom"/>
<Borders/>
<Font ss:FontName="Calibri" x:CharSet="238" x:Family="Swiss" ss:Size="11" ss:Color="#000000"/>
<Interior/>
<NumberFormat/>
<Protection/>
</Style>
</Styles>
<Worksheet ss:Name="Sheet1">
<Table ss:ExpandedColumnCount="1" ss:ExpandedRowCount="1" x:FullColumns="1" x:FullRows="1" ss:DefaultRowHeight="15"/>
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<PageSetup>
<Header x:Margin="0.3"/>
<Footer x:Margin="0.3"/>
<PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75"/>
</PageSetup>
<Selected/>
<Panes>
<Pane>
<Number>3</Number>
<ActiveCol>1</ActiveCol>
</Pane>
</Panes>
<ProtectObjects/>
<ProtectScenarios/>
</WorksheetOptions>
</Worksheet>
<Worksheet ss:Name="Sheet2">
<Table ss:ExpandedColumnCount="1" ss:ExpandedRowCount="1" x:FullColumns="1" x:FullRows="1" ss:DefaultRowHeight="15"/>
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<PageSetup>
<Header x:Margin="0.3"/>
<Footer x:Margin="0.3"/>
<PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75"/>
</PageSetup>
<Selected/>
<Panes>
<Pane>
<Number>3</Number>
<ActiveCol>1</ActiveCol>
</Pane>
</Panes>
<ProtectObjects/>
<ProtectScenarios/>
</WorksheetOptions>
</Worksheet>
<Worksheet ss:Name="Sheet3">
<Table ss:ExpandedColumnCount="1" ss:ExpandedRowCount="1" x:FullColumns="1" x:FullRows="1" ss:DefaultRowHeight="15"/>
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<PageSetup>
<Header x:Margin="0.3"/>
<Footer x:Margin="0.3"/>
<PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75"/>
</PageSetup>
<Selected/>
<Panes>
<Pane>
<Number>3</Number>
<ActiveCol>1</ActiveCol>
</Pane>
</Panes>
<ProtectObjects/>
<ProtectScenarios/>
</WorksheetOptions>
</Worksheet>
</Workbook>
我想使用 XPath 获取具有属性“Sheet1”的 Worksheet 元素。这就是我所拥有的:
$uri = $this->doc->getDocNamespaces()['']; //$this->doc is obiect of simplexmlelement class
$this->doc->registerXPathNamespace('default', $uri); //'urn:schemas-microsoft-com:office:spreadsheet'
$current_worksheet = $this->doc->xpath('/*/default:Worksheet[@ss:Name = "Sheet1"]');
die(var_dump($current_worksheet));//empty array :(
现在 $current_worksheet 是一个空数组 :( 看起来默认命名空间与 ss 命名空间相同(相同的 urn)?