duf*_*ffn 7 xml sql-server sql-server-2008-r2
declare @xmlsample xml =
'<root>
<solution>
<solutionnumber>1</solutionnumber>
<productgroup>
<productcategory>
<price>100</price>
<title>Some product</title>
<tax>1</tax>
</productcategory>
</productgroup>
<productcategory2>
<price>200</price>
<title>Some other product</title>
<tax>2</tax>
</productcategory2>
</solution>
<solution>
<solutionnumber>2</solutionnumber>
<productcategory2>
<price>200</price>
<title>Some other product</title>
<tax>2</tax>
</productcategory2>
</solution>
</root>'
SELECT
--T.C.value('(./ancestor::ns1:solutionNumber)[1]', 'varchar(50)') AS solutionnumber ?? no clue
T.C.value('(price)[1]', 'numeric(18,2)') AS price
,T.C.value('(title)[1]', 'varchar(50)') AS title
,T.C.value('(tax)[1]', 'numeric(18,2)') AS tax
FROM @xmlsample.nodes('//node()[title]') AS T(C)
Run Code Online (Sandbox Code Playgroud)
我试图在SQL Server 2008 r2中粉碎的XML的表示.我找到了"标题"节点,并获取了产品类别中我需要的值.现在我想获得"解决方案编号",但是这可能是产品上方的一个或多个父节点,因为存在某些产品"组".
在找到之前,我将如何通过名称("solutionnumber")检查父节点?谢谢你的帮助.
没有直接的途径了解我的知识。但是,您可以使用 COALESCE 来向上搜索:
SELECT
COALESCE(T.C.value('../solutionnumber[1]', 'INT'),
T.C.value('../../solutionnumber[1]', 'INT'),
T.C.value('../../../solutionnumber[1]', 'INT')) solutionnumber,
T.C.value('(price)[1]', 'numeric(18,2)') AS price,
T.C.value('(title)[1]', 'varchar(50)') AS title,
T.C.value('(tax)[1]', 'numeric(18,2)') AS tax
FROM
@xmlsample.nodes('//node()[title]') AS T ( C )
Run Code Online (Sandbox Code Playgroud)
请注意,<solutionnumber
> 实际上是祖先之一的兄弟姐妹,而不是祖先本身。
此解决方案要求您提前知道最大深度。
如果您宁愿前进而不是后退,也可以使用此解决方案:
SELECT solutionNodes.solutionNode.value('solutionnumber[1]','INT') AS solutionnumber,
T.C.value('(price)[1]', 'numeric(18,2)') AS price,
T.C.value('(title)[1]', 'varchar(50)') AS title,
T.C.value('(tax)[1]', 'numeric(18,2)') AS tax
FROM @xmlsample.nodes('//solution') AS solutionNodes (solutionNode)
CROSS APPLY (SELECT solutionNodes.solutionNode.query('.')) solutions(solutionXML)
CROSS APPLY solutions.solutionXML.nodes('//node()[title]') T ( C )
Run Code Online (Sandbox Code Playgroud)
<solutionnumber
它利用了> 标记是 > 标记的直接子级这一事实<solution
。<solution
首先找到所有> 标签。比其所有头衔后代都通过交叉应用找到。因为不能在节点上使用节点函数,所以中间需要计算“query('.')”。
除了上面的解决方案之外,这个解决方案可以处理<solution
> 标签和<title
> 标签之间的任何距离。
也许我是在倒退。多个交叉应用即可完成这项工作。感谢另一个论坛上的一些帮助。
SELECT
--T.C.value('(./ancestor::ns1:solutionNumber)[1]', 'varchar(50)') AS solutionnumber ?? no clue
m.c.value('(solutionnumber)[1]', 'int') as solutionnumber
,T.C.value('(price)[1]', 'numeric(18,2)') AS price
,T.C.value('(title)[1]', 'varchar(50)') AS title
,T.C.value('(tax)[1]', 'numeric(18,2)') AS tax
FROM @xmlsample.nodes ('//solution') as m (c)
cross apply m.c.nodes ('.//node()[title]') as t(C)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
13472 次 |
最近记录: |