本文介紹了在忽略命名空間的同時(shí)查詢 XML?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問(wèn)題描述
限時(shí)送ChatGPT賬號(hào)..
我試圖在忽略命名空間的同時(shí)查詢 XML,因?yàn)榻Y(jié)果集有多個(gè)命名空間.我已經(jīng)到了 DataSets 節(jié)點(diǎn),但我不知道如何取出多個(gè) DataSourceName/CommandType/CommandText.理想情況下,我想要:
I'm trying to query XML while ignoring namespaces, because the result set has multiple namespaces. I've gotten to the DataSets node, but I can't figure out how to get out the multiple DataSourceName/CommandType/CommandText. Ideally I want:
DataSetName DataSourceName CommandType CommandText
SQLDS SQLDS StoredProcedure ReportProc_aaaaa
SQLDS SQLDS StoredProcedure ReportProc_lalala
非常感謝幫助.
DECLARE @xmltable TABLE (myxml XML)
INSERT INTO @xmltable
SELECT
'<Report xmlns="http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition" xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportdesigner">
<DataSources>
<DataSource Name="SQLDS">
<rd:DataSourceID>32e83b35-434d-4808-b685-ada14accd0e7</rd:DataSourceID>
<DataSourceReference>SQLDS</DataSourceReference>
</DataSource>
</DataSources>
<DataSets>
<DataSet Name="SQLDS">
<Query>
<DataSourceName>SQLDS</DataSourceName>
<CommandType>StoredProcedure</CommandType>
<CommandText>ReportProc_ServerPerformanceGroup</CommandText>
</Query>
</DataSet>
<DataSet Name="GroupDetails">
<Query>
<DataSourceName>SQLDS</DataSourceName>
<CommandType>StoredProcedure</CommandType>
<CommandText>ReportProc_lalala</CommandText>
</Query>
</DataSet>
</DataSets>
</Report>'
SELECT myxml.value('(/*:Report/*:DataSets)[1]','varchar(100)') FROM @xmltable
推薦答案
使用 nodes() 方法(xml 數(shù)據(jù)類型) 將 yoru XML 分解為行并使用 value() 方法(xml 數(shù)據(jù)類型) 從 XML 中獲取特定值.
Use nodes() Method (xml Data Type) to shred yoru XML to rows and use value() Method (xml Data Type) to get specific values from the XML.
select T1.N.value('@Name', 'nvarchar(128)') as DataSetName,
T2.N.value('(*:DataSourceName/text())[1]', 'nvarchar(128)') as DataSourceName,
T2.N.value('(*:CommandType/text())[1]', 'nvarchar(128)') as CommandType,
T2.N.value('(*:CommandText/text())[1]', 'nvarchar(max)') as CommandText
from @xmltable as T
cross apply T.myxml.nodes('/*:Report/*:DataSets/*:DataSet') as T1(N)
cross apply T1.N.nodes('*:Query') as T2(N)
SQL 小提琴
這篇關(guān)于在忽略命名空間的同時(shí)查詢 XML?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!