1

I would like to access the value of each cell of nodelists I retrieved with:

$xc->registerNs('ns', 'urn:schemas-microsoft-com:office:spreadsheet');
foreach my $row ($xc->findnodes('/ns:Workbook/ns:Worksheet/ns:Table/ns:Row'))

they have the following format:

<Row>
    <Cell><Data ss:Type="String">sbdsvrwm832</Data></Cell>
    <Cell><Data ss:Type="String">3.0.6</Data></Cell>
    <Cell><Data ss:Type="Number">5.0999999999999996</Data></Cell>
    <Cell><Data ss:Type="String"/></Cell>
    <Cell><Data ss:Type="String">NA</Data></Cell>
    <Cell><Data ss:Type="String">NA</Data></Cell>
    <Cell><Data ss:Type="String">N/A in SHV3</Data></Cell>
    <Cell><Data ss:Type="String">YES</Data></Cell>
    <Cell><Data ss:Type="String"/></Cell>
    <Cell><Data ss:Type="String">YES</Data></Cell>
    <Cell><Data ss:Type="String">2013-09-20</Data></Cell>
    <Cell><Data ss:Type="String">NO</Data></Cell>
    <Cell><Data ss:Type="String"/></Cell>
   </Row><Row>
    <Cell><Data ss:Type="String">sbtorsvr832</Data></Cell>
    <Cell><Data ss:Type="String">4.0.1</Data></Cell>
    <Cell><Data ss:Type="Number">3.011111111</Data></Cell>
    <Cell><Data ss:Type="String"/></Cell>
    <Cell><Data ss:Type="String">ORI</Data></Cell>
    <Cell><Data ss:Type="String">NA</Data></Cell>
    <Cell><Data ss:Type="String">N/A in SHV3</Data></Cell>
    <Cell><Data ss:Type="String">YES</Data></Cell>
    <Cell><Data ss:Type="String"/></Cell>
    <Cell><Data ss:Type="String">YES</Data></Cell>
    <Cell><Data ss:Type="String">2013-07-18</Data></Cell>
    <Cell><Data ss:Type="String">NO</Data></Cell>
    <Cell><Data ss:Type="String"/></Cell>
   </Row>...
...

Note that the Cell/Data elements do NOT have names or IDs that can be used with XPath expressions (they are mostly identical elements, some values may be null or contain whitespaces). Is there a way I can access data of an arbitrary cell for each of such row by index? Thanks in advance.

Alex

4

1 回答 1

0

您正在寻找的 XPath 表达式是(相对于Row元素)...

"Cell[position()=$i]/Data"

...其中$i对应于您要查找的元素的索引(从 1 开始!)。

如果您需要为所有行检索具有相同位置的所有项目,只需在此表达式前面加上//Row/(假设您从根元素查询)。例如:

my @nodesOfColumn2 = $dom->findnodes('//Row/Cell[position()=2]/Data');
print $_->to_literal, "\n" for @nodesOfColumn2;
### prints... 
### 3.0.6
### 4.0.1 
于 2013-09-22T21:24:18.557 回答