DECLARE @xml xml=' <?xml version="1.0" encoding="UTF-8"?><text>
<table>
<thead>
<tr>
<th>a</th>
<th>b</th>
<th>c</th>
<th>d</th>
<th>e</th>
<th>f</th>
</tr>
</thead>
<tbody>
<tr>
<td>testa1</td>
<td>testb1</td>
<td>testc1</td>
<td>testd1</td>
<td>teste1</td>
<td>testf1</td>
</tr>
<tr>
<td>testa1</td>
<td>testb1</td>
<td>testc1</td>
<td>testd1</td>
<td>teste1</td>
<td>testf1</td>
</tr>
</tbody>
</table>
</text>'
SELECT
T.c.value('(tbody/tr/td)[1]','VARCHAR(100)') AS a
FROM
@xml.nodes('text/table[thead[tr[th="b"]]]') AS t(c)
此查询仅从xml中选择第一个正文内容。
这里我只想选择标题b
下的值答案 0 :(得分:0)
修改xpath如下:
SELECT T.c.value('/text/table/tbody/tr/td[position()=2]','VARCHAR(100)') AS a
说明:
选择兄弟姐妹列表中的第二个td
元素。
注意:
将其与this SO answer结合使用以获取:
SELECT T.c.value('/text/table/tbody/tr/td[position() = count(/text/table/tbody/tr/th[text() = "b"]/preceding-sibling::*)+1]','VARCHAR(100)') AS a