Sql 如何从多个同名XML节点返回行?
以下XML在节点“attachDocument”下有多个节点。下面的SQL查询只返回“attachDocument”的第一个节点的行,而不是第二个节点的行 是否有人可以帮助从“attachDocument”的两个节点返回行 先谢谢你 XMLSql 如何从多个同名XML节点返回行?,sql,xml,Sql,Xml,以下XML在节点“attachDocument”下有多个节点。下面的SQL查询只返回“attachDocument”的第一个节点的行,而不是第二个节点的行 是否有人可以帮助从“attachDocument”的两个节点返回行 先谢谢你 XML set @sqlxml = N'<response xmlns="http://xyz.in/twa/cmm/decl/v2"> <identification>88762431</identificatio
set @sqlxml = N'<response xmlns="http://xyz.in/twa/cmm/decl/v2">
<identification>88762431</identification>
<type>RESPONSE</type>
<submitter><identifier>40134916C</identifier></submitter>
<functionalReference>TSW07389555IM1</functionalReference>
<transactionType>24</transactionType>
<attachDocument>
<category>AAA</category>
<mimeCode>application/pdf</mimeCode>
<URI>16f15574-5d5a-4e83-b9ac-2151f10cf2eb</URI>
<filename>XYZ_B2021_199.pdf</filename>
</attachDocument>
<attachDocument>
<category>AAB</category>
<mimeCode>text/plain</mimeCode>
<URI>1511b476-a2be-4ae5-a54c-0a5dc14759b2</URI>
<filename>XYZ_B2021_199_xml.txt</filename>
</attachDocument>
<additionalInformationICN><text>Please refer to attached XYZ for Directions</text></additionalInformationICN>
<issueDate>20210331113355</issueDate>
<overallDeclaration>
<identification>88762431</identification>
<functionalReference>TSW07389555IM1</functionalReference>
<submitter>
<identifier>40134916C</identifier>
</submitter>
<responsibleGovernmentAgency>XYZ</responsibleGovernmentAgency>
</overallDeclaration>
<status>
<agency>XYZ</agency>
<effectiveDate>20210331113355</effectiveDate>
<name>B04</name>
<releaseDate>20210331113355</releaseDate>
</status>
</response>'
SQL Query...
;WITH XMLNAMESPACES ('http://xyz.in/twa/cmm/decl/v2' AS ur)
SELECT
response.value('(//ur:attachDocument/ur:category)[1]','varchar(50)') as Category,
response.value('(//ur:attachDocument/ur:mimeCode)[1]','varchar(50)') as MimeCode,
response.value('(//ur:attachDocument/ur:URI)[1]','varchar(50)') as URI,
response.value('(//ur:attachDocument/ur:filename)[1]','varchar(50)') as [FileName],
response.value('(//ur:status/ur:agency)[1]','varchar(100)') as ResponseAgency,
response.value('(//ur:response/ur:issueDate)[1]','varchar(100)') as ResponseIssueDateTime,
response.value('(//ur:response/ur:additionalInformationICN)[1]','varchar(100)') as ResponseClearanceInstructions
FROM @sqlxml.nodes('//ur:response') AS T(response)
set@sqlxml=N'
88762431
回应
40134916C
TSW07389555IM1
24
AAA
申请表格/pdf
16f15574-5d5a-4e83-b9ac-2151f10cf2eb
XYZ_B2021_199.pdf
AAB
文本/纯文本
1511b476-a2be-4ae5-a54c-0a5dc14759b2
XYZ_B2021_199_xml.txt
有关说明,请参阅随附的XYZ
20210331113355
88762431
TSW07389555IM1
40134916C
XYZ
XYZ
20210331113355
B04
20210331113355
'
SQL查询。。。
;使用XMLNAMESPACES('http://xyz.in/twa/cmm/decl/v2"如你所愿)
挑选
response.value(“(//ur:attachDocument/ur:category)[1]”,“'varchar(50)”作为类别,
response.value(“(//ur:attachDocument/ur:mimeCode)[1]”,“'varchar(50)”作为mimeCode,
value(“(//ur:attachDocument/ur:URI)[1]”,'varchar(50)”作为URI,
response.value(“(//ur:attachDocument/ur:filename)[1]”,'varchar(50)”作为[filename],
response.value(“(//ur:status/ur:agency)[1]”,“'varchar(100)”作为ResponseAgency,
response.value(“(//ur:response/ur:issueDate)[1]”,“'varchar(100)”作为responseIsueDateTime,
response.value(“(//ur:response/ur:additionalInformationICN)[1]”,“'varchar(100)”作为ResponseClearanceInstructions
从@sqlxml.nodes('//ur:response')作为T(response)
请尝试以下解决方案
值得注意的是:
- 正确声明默认名称空间可以全面简化XPath表达式。默认命名空间不需要前缀
- 最好不要在XPath表达式中使用
。它会导致遍历整个XML。完全限定的路径对性能更好/
- 将text()添加到XML元素的XPath表达式中可以提高性能
DECLARE @sqlxml XML =
N'<response xmlns="http://xyz.in/twa/cmm/decl/v2">
<identification>88762431</identification>
<type>RESPONSE</type>
<submitter>
<identifier>40134916C</identifier>
</submitter>
<functionalReference>TSW07389555IM1</functionalReference>
<transactionType>24</transactionType>
<attachDocument>
<category>AAA</category>
<mimeCode>application/pdf</mimeCode>
<URI>16f15574-5d5a-4e83-b9ac-2151f10cf2eb</URI>
<filename>XYZ_B2021_199.pdf</filename>
</attachDocument>
<attachDocument>
<category>AAB</category>
<mimeCode>text/plain</mimeCode>
<URI>1511b476-a2be-4ae5-a54c-0a5dc14759b2</URI>
<filename>XYZ_B2021_199_xml.txt</filename>
</attachDocument>
<additionalInformationICN>
<text>Please refer to attached XYZ for Directions</text>
</additionalInformationICN>
<issueDate>20210331113355</issueDate>
<overallDeclaration>
<identification>88762431</identification>
<functionalReference>TSW07389555IM1</functionalReference>
<submitter>
<identifier>40134916C</identifier>
</submitter>
<responsibleGovernmentAgency>XYZ</responsibleGovernmentAgency>
</overallDeclaration>
<status>
<agency>XYZ</agency>
<effectiveDate>20210331113355</effectiveDate>
<name>B04</name>
<releaseDate>20210331113355</releaseDate>
</status>
</response>'
;WITH XMLNAMESPACES (DEFAULT 'http://xyz.in/twa/cmm/decl/v2')
SELECT response.value('(category/text())[1]','varchar(50)') as Category,
response.value('(mimeCode/text())[1]','varchar(50)') as MimeCode,
response.value('(URI/text())[1]','varchar(50)') as URI,
response.value('(filename/text())[1]','varchar(50)') as [FileName],
response.value('(/response/status/agency/text())[1]','varchar(100)') as ResponseAgency,
response.value('(/response/issueDate/text())[1]','varchar(100)') as ResponseIssueDateTime,
response.value('(/response/additionalInformationICN/text/text())[1]','varchar(100)') as ResponseClearanceInstructions
FROM @sqlxml.nodes('/response/attachDocument') AS T(response);
这是完美的作品。。。谢谢伊扎克。。非常感谢…@SQLHoncho,很高兴听到提议的解决方案对您有效。请向上投票建议:@SQLHoncho,请在LinkedIn上与我联系。请参考以上XML脚本。。。节点“additionalInformationICN”下存在相同的节点“AttachDocument”,如下所示。。。正在努力添加到上面的SQL脚本以返回它。有人能帮忙吗?请参考随附的XYZ,了解ABC文本/普通1511b476-a2ef-6ae5-a54c-0a5dc14759b2 DEF_B2019_199_xml.txt的说明
+----------+-----------------+--------------------------------------+-----------------------+----------------+-----------------------+---------------------------------------------+
| Category | MimeCode | URI | FileName | ResponseAgency | ResponseIssueDateTime | ResponseClearanceInstructions |
+----------+-----------------+--------------------------------------+-----------------------+----------------+-----------------------+---------------------------------------------+
| AAA | application/pdf | 16f15574-5d5a-4e83-b9ac-2151f10cf2eb | XYZ_B2021_199.pdf | XYZ | 20210331113355 | Please refer to attached XYZ for Directions |
| AAB | text/plain | 1511b476-a2be-4ae5-a54c-0a5dc14759b2 | XYZ_B2021_199_xml.txt | XYZ | 20210331113355 | Please refer to attached XYZ for Directions |
+----------+-----------------+--------------------------------------+-----------------------+----------------+-----------------------+---------------------------------------------+