問題描述
我有這個(gè) XPath 表達(dá)式,它對(duì)最深的 XML 層次結(jié)構(gòu)中的所有節(jié)點(diǎn)求和,而不使用節(jié)點(diǎn)名稱:
I have this XPath expression, which sum all nodes in the deepest XML hierarchy, without using nodes names:
select @data.value('sum(//*[not(*)])', 'float')
如何根據(jù)名稱對(duì)一個(gè)節(jié)點(diǎn)進(jìn)行例外處理?
How do I make an exception of one node, by its name?
說(shuō)這是xml:
<c>
<b1>
<a>1</a>
<d>4</d>
<g>5</g>
</b1>
<b1>
<a>7</a>
<d>1</d>
<g>2</g>
</b1>
</c>
我希望總和包含d"和g",沒有a",但a"將作為參數(shù)傳遞,因此需要在表達(dá)式中表示為參數(shù).我嘗試了以下方法:
I would like the sum to contain "d" and "g", without "a", but "a" will be pass as parameter and so need to be represented as parameter inside the expression. I've tried the following:
declare @except varchar(max) = 'a'
select @data.value('sum(//*[not(*)])', 'float') - @data.value('sum(//*:local-name()=sql:variable("@except"))', 'float')
但沒有成功.
推薦答案
雖然 Marc 的回答是我最會(huì)做的,但我有一個(gè)小技巧給你,下面
While Marc's answer is what i would do mostly, I have a small hack for you, below
select @data.value('sum(//*[not(*)])', 'float')- @data.value('sum(//*:a)', 'float')
這篇關(guān)于從 sum 中刪除一個(gè) xml 節(jié)點(diǎn),在 XPath 到 SQL Server XML 列中的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!