我有一個奇怪的問題。是否可以從下面顯示的 XML 中獲取按attval節點過濾的節點值?attrno
示例:我需要獲取12 和 2的attval值:attrno
<attributes>
<attribute>
<attrno>2</attrno>
<attval>NA</attval>
</attribute>
<attribute>
<attrno>11</attrno>
<attval>0123456</attval>
</attribute>
<attribute>
<attrno>12</attrno>
<attval>2210</attval>
</attribute>
<attribute>
<attrno>10</attrno>
<attval>0.049833</attval>
</attribute>
<attribute>
<attrno>11</attrno>
<attval>EA</attval>
</attribute>
<attribute>
<attrno>12</attrno>
<attval>EUR</attval>
</attribute>
</attributes>
這是我嘗試使用的 T-SQL 查詢:
INSERT INTO @temp (id, skid, Attval)
SELECT
ROW_NUMBER() OVER (ORDER BY (SELECT 1)),
col.value('(../../tcid/text())[1]', 'VARCHAR(200)'),
col.value('(attval/text())[1]', 'VARCHAR(200)')
FROM
@DataRecXML.nodes('/rec/tcids/tcid_rec/attributes/attribute') AS tb(col)
WHERE
col.value('(attrno/text())[1]', 'VARCHAR(200)') = 2
OR col.value('(attrno/text())[1]', 'VARCHAR(200)') = 12
AND col.value ('(attval/text())[1]', 'VARCHAR(200)') <> 'EUR'
感謝您的任何幫助 :)
uj5u.com熱心網友回復:
請嘗試以下解決方案。
過濾器應用在 XPath 謂詞中。它使代碼更加緊湊和高性能。
SQL
DECLARE @DataRecXML XML =
N'<attributes>
<attribute>
<attrno>2</attrno>
<attval>NA</attval>
</attribute>
<attribute>
<attrno>11</attrno>
<attval>0123456</attval>
</attribute>
<attribute>
<attrno>12</attrno>
<attval>2210</attval>
</attribute>
<attribute>
<attrno>10</attrno>
<attval>0.049833</attval>
</attribute>
<attribute>
<attrno>11</attrno>
<attval>EA</attval>
</attribute>
<attribute>
<attrno>12</attrno>
<attval>EUR</attval>
</attribute>
</attributes>';
SELECT id = ROW_NUMBER() OVER (ORDER BY (SELECT 1)),
attrno = col.value('(attrno/text())[1]', 'INT'),
attval = col.value('(attval/text())[1]', 'VARCHAR(200)')
FROM @DataRecXML.nodes('/attributes/attribute[(attrno/text())[1]=("2","12")
and (attval/text())[1] ne "EUR"]') AS tb(col);
輸出
---- -------- --------
| id | attrno | attval |
---- -------- --------
| 1 | 2 | NA |
| 2 | 12 | 2210 |
---- -------- --------
uj5u.com熱心網友回復:
評論太長了。這是對請求的行為進行建模的示例。輸出有什么問題?
select id,
a.n.value('attrno[1]', 'int') attrno,
a.n.value('attval[1]', 'varchar(20)') attval
from -- your table here
( select 1 id, cast(
'<attributes>
<attribute>
<attrno>2</attrno>
<attval>NA</attval>
</attribute>
<attribute>
<attrno>11</attrno>
<attval>0123456</attval>
</attribute>
<attribute>
<attrno>12</attrno>
<attval>2210</attval>
</attribute>
<attribute>
<attrno>10</attrno>
<attval>0.049833</attval>
</attribute>
<attribute>
<attrno>11</attrno>
<attval>EA</attval>
</attribute>
<attribute>
<attrno>12</attrno>
<attval>EUR</attval>
</attribute>
</attributes>'
as xml) xcol
) tbl
cross apply xcol.nodes('attributes/attribute') a(n)
where a.n.value('attrno[1]', 'int') = 2
or a.n.value('attrno[1]', 'int') = 12 and a.n.value('attval[1]', 'varchar(20)') <> 'EUR'
輸出
id attrno attval
1 2 NA
1 12 2210
uj5u.com熱心網友回復:
@YitzhakKhabinsky 的出色答案的語法略有不同。很難說哪個更快,你可能需要測驗
SELECT id = ROW_NUMBER() OVER (ORDER BY (SELECT 1)),
attrno = col.value('(attrno/text())[1]', 'INT'),
attval = col.value('(attval/text())[1]', 'VARCHAR(200)')
FROM @DataRecXML.nodes('
/attributes/attribute
[attrno/text() = ("2", "12")]
[not(attval/text() = "EUR")]') AS tb(col);
db<>小提琴
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/481312.html
上一篇:使用變數進行XSL分組
下一篇:Xml追加/添加新節點php
