【发布时间】:2013-04-18 11:19:13
【问题描述】:
我有这样的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命名空间一样(同一个瓮)?
【问题讨论】:
-
"dirty hack" is with
local-name(),对于属性,您需要注册一个前缀,以便您可以将它与您的 xpath 一起使用。此外,您的 XML 文件 无效,根元素之前不能有空格,我将在您的问题中修复它,删除空格。 -
这个xml只是简化,复制粘贴而成
-
<Workbook>是文档元素吗?如果不是,这确实会有所不同,因此您应该明确说明,否则您要求的是错误的东西;)(我不是在问这是否是整个文档,只是第一个element 也是原始 XML 中的第一个元素) -
仔细检查您的示例。使用 XML 和您的代码(simplexml),它确实对我有用。你也不需要注册 default: 因为你可以使用 ss: 代替你写的。这也是我在回答中概述的方式。您可能在其他地方有一个小错误,xpath 有效:(
-
嗯...问题可能出在我将这个属性添加到 xml 的方式上,我这样做是这样的 $ws = $this->doc->addChild('Worksheet'); $ws->addAttribute('xmlns:ss:Name', 'Sheet1');你怎么看?