使用 XQuery 提取 HTML table 的所有行和列(及其行跨度和列跨度)

Extracting all rows and columns (with their rowspans and colspans) of an HTML table with XQuery

我正在尝试使用 XQuery 提取 HTML table 的单元格中的所有值。我正在使用的查询(您可以在下面找到)给出以下结果

Warning on line 11 column 22 of queryExtractTable.xq:
  The child axis starting at an attribute node node will never select anything
Warning on line 11 column 63 of queryExtractTable.xq:
  The child axis starting at an attribute node node will never select anything
<?xml version="1.0" encoding="UTF-8"?>hello colspan rowspan

我不明白为什么 "The child axis starting at an attribute node node will never select anything"。

我正在使用 Saxon。

这里是查询

declare default element namespace "http://www.w3.org/1999/xhtml";


declare function local:analyzeTable(
$table as element(table))
{
    for $r in $table//tr
        return
            for $c in $r//td
                    return (normalize-space($c), string("colspan"),
$c/@colspan//text() , string("rowspan"), $c/@rowspan//text() )

};


for $t in //table
    return
        local:analyzeTable($t)

table

<table>
    <tr>
        <td colspan="2">hello</td>
    </tr>
</table>

警告由如下表达式引发:

$c/@colspan//text()

@colspan为属性节点,属性节点没有子节点。因此,当您请求属性的后代 text() 节点时,Saxon 会发出警告。

要访问这些属性的字符串值,您可以将这些表达式更改为:

string($c/@colspan)

我发现您已经熟悉 string() 函数,例如 string("colspan");请注意,这里的 string() 函数是无关紧要的,"colspan" 足以构造一个文字字符串。

有关 text()string()data() 的更多信息,请参阅 https://developer.marklogic.com/blog/text-is-a-code-smell