如何从 SQL Server 2019 (v15) 中的 XML 中提取属性值?
How to extract attribute value from XML in SQL Server 2019 (v15)?
我需要将此 XML 中的元素提取到表格形式中,但我似乎无法理解这将如何通过 XQuery 之类的东西在 SQL 服务器上工作。
我将所有数据都放在一个名为“#1”的临时 table 中,XML 本身位于该临时 table 中一个名为“消息”的字段中。如何将值“Test1”和“2,2 %”提取到分别称为“W08003”和“W1A081”的单独字段中?属性名称和架构会随着时间的推移保持不变吗?我还需要对当前临时 table.
中的每个 XML 逐行执行此操作
<Individual xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Content>
<status xmlns:d3p1="http://www.uc.se/schemas/ucOrderReply/" xmlns="http://www.uc.se/schemas/ucOrderReply/" d3p1:result="ok" />
<uc xmlns="http://www.uc.se/schemas/ucOrderReply/">
<xmlReply>
<reports xmlns:d5p1="http://www.uc.se/schemas/ucOrderReply/" d5p1:lang="eng">
<d5p1:report d5p1:id="7605089247" d5p1:name="Test1 Test2" d5p1:styp="K39" d5p1:index="0">
<d5p1:group d5p1:id="W080" d5p1:index="0" d5p1:key="" d5p1:name="ID particulars">
<d5p1:term d5p1:id="W08001">9760508923</d5p1:term>
<d5p1:term d5p1:id="W08002">7605089277</d5p1:term>
<d5p1:term d5p1:id="W08003">Test1</d5p1:term>
<d5p1:term d5p1:id="W08004">Test2</d5p1:term>
</d5p1:group>
<d5p1:group d5p1:id="W1A0" d5p1:index="0" d5p1:key="" d5p1:name="UC RPB">
<d5p1:term d5p1:id="W1A003">000000000000000022</d5p1:term>
<d5p1:term d5p1:id="W1A081">2,2 %</d5p1:term>
<d5p1:term d5p1:id="W1A082">2,18839</d5p1:term>
</d5p1:group>
</d5p1:report>
</reports>
</xmlReply>
</uc>
</Content>
</Individual>
当前SQL代码:
WITH XMLNAMESPACES('http://www.uc.se/schemas/ucOrderReply/' AS ns,'http://www.uc.se/schemas/ucOrderReply/' AS d5p1)
SELECT ok.*
,X.g.value('(@d5p1:id)','varchar(20)') AS id
,X.g.value('(text())[1]','varchar(20)') AS term
into #2
FROM #1 as ok
CROSS APPLY(ok.[Message].nodes('Individual/Content/ns:uc/ns:xmlReply/ns:reports/ns:report/ns:group/ns:term') X(g)
没有预期的结果,也许这足以让你开始。
因为你只在到达 status
时才定义默认命名空间,你不能在 XMLNAMESPACES
中使用 DEFAULT
命名空间,所以我将其命名为 ns
并引用它。这为您提供了所有 term
及其 id
属性的值:
DECLARE @XML xml = '<Individual xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Content>
<status xmlns:d3p1="http://www.uc.se/schemas/ucOrderReply/" xmlns="http://www.uc.se/schemas/ucOrderReply/" d3p1:result="ok" />
<uc xmlns="http://www.uc.se/schemas/ucOrderReply/">
<xmlReply>
<reports xmlns:d5p1="http://www.uc.se/schemas/ucOrderReply/" d5p1:lang="eng">
<report d5p1:id="7605089247" d5p1:name="Test1 Test2" d5p1:styp="K39" d5p1:index="0">
<group d5p1:id="W080" d5p1:index="0" d5p1:key="" d5p1:name="ID particulars">
<term d5p1:id="W08001">9760508923</term>
<term d5p1:id="W08002">7605089277</term>
<term d5p1:id="W08003">Test1</term>
<term d5p1:id="W08004">Test2</term>
</group>
<group d5p1:id="W1A0" d5p1:index="0" d5p1:key="" d5p1:name="UC RPB">
<term d5p1:id="W1A003">000000000000000022</term>
<term d5p1:id="W1A081">2,2 %</term>
<term d5p1:id="W1A082">2,18839</term>
</group>
</report>
</reports>
</xmlReply>
</uc>
</Content>
</Individual>';
WITH XMLNAMESPACES('http://www.uc.se/schemas/ucOrderReply/' AS ns,'http://www.uc.se/schemas/ucOrderReply/' AS d5p1)
SELECT X.g.value('(@d5p1:id)','varchar(20)') AS id,
X.g.value('(text())[1]','varchar(20)') AS term
FROM @XML.nodes('Individual/Content/ns:uc/ns:xmlReply/ns:reports/ns:report/ns:group/ns:term') X(g);
我注意到 XML 自从我用来编写此答案的初始版本以来已经更改。此答案尚未(读作“不会”)为此进行调整。
我需要将此 XML 中的元素提取到表格形式中,但我似乎无法理解这将如何通过 XQuery 之类的东西在 SQL 服务器上工作。
我将所有数据都放在一个名为“#1”的临时 table 中,XML 本身位于该临时 table 中一个名为“消息”的字段中。如何将值“Test1”和“2,2 %”提取到分别称为“W08003”和“W1A081”的单独字段中?属性名称和架构会随着时间的推移保持不变吗?我还需要对当前临时 table.
中的每个 XML 逐行执行此操作<Individual xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Content>
<status xmlns:d3p1="http://www.uc.se/schemas/ucOrderReply/" xmlns="http://www.uc.se/schemas/ucOrderReply/" d3p1:result="ok" />
<uc xmlns="http://www.uc.se/schemas/ucOrderReply/">
<xmlReply>
<reports xmlns:d5p1="http://www.uc.se/schemas/ucOrderReply/" d5p1:lang="eng">
<d5p1:report d5p1:id="7605089247" d5p1:name="Test1 Test2" d5p1:styp="K39" d5p1:index="0">
<d5p1:group d5p1:id="W080" d5p1:index="0" d5p1:key="" d5p1:name="ID particulars">
<d5p1:term d5p1:id="W08001">9760508923</d5p1:term>
<d5p1:term d5p1:id="W08002">7605089277</d5p1:term>
<d5p1:term d5p1:id="W08003">Test1</d5p1:term>
<d5p1:term d5p1:id="W08004">Test2</d5p1:term>
</d5p1:group>
<d5p1:group d5p1:id="W1A0" d5p1:index="0" d5p1:key="" d5p1:name="UC RPB">
<d5p1:term d5p1:id="W1A003">000000000000000022</d5p1:term>
<d5p1:term d5p1:id="W1A081">2,2 %</d5p1:term>
<d5p1:term d5p1:id="W1A082">2,18839</d5p1:term>
</d5p1:group>
</d5p1:report>
</reports>
</xmlReply>
</uc>
</Content>
</Individual>
当前SQL代码:
WITH XMLNAMESPACES('http://www.uc.se/schemas/ucOrderReply/' AS ns,'http://www.uc.se/schemas/ucOrderReply/' AS d5p1)
SELECT ok.*
,X.g.value('(@d5p1:id)','varchar(20)') AS id
,X.g.value('(text())[1]','varchar(20)') AS term
into #2
FROM #1 as ok
CROSS APPLY(ok.[Message].nodes('Individual/Content/ns:uc/ns:xmlReply/ns:reports/ns:report/ns:group/ns:term') X(g)
没有预期的结果,也许这足以让你开始。
因为你只在到达 status
时才定义默认命名空间,你不能在 XMLNAMESPACES
中使用 DEFAULT
命名空间,所以我将其命名为 ns
并引用它。这为您提供了所有 term
及其 id
属性的值:
DECLARE @XML xml = '<Individual xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Content>
<status xmlns:d3p1="http://www.uc.se/schemas/ucOrderReply/" xmlns="http://www.uc.se/schemas/ucOrderReply/" d3p1:result="ok" />
<uc xmlns="http://www.uc.se/schemas/ucOrderReply/">
<xmlReply>
<reports xmlns:d5p1="http://www.uc.se/schemas/ucOrderReply/" d5p1:lang="eng">
<report d5p1:id="7605089247" d5p1:name="Test1 Test2" d5p1:styp="K39" d5p1:index="0">
<group d5p1:id="W080" d5p1:index="0" d5p1:key="" d5p1:name="ID particulars">
<term d5p1:id="W08001">9760508923</term>
<term d5p1:id="W08002">7605089277</term>
<term d5p1:id="W08003">Test1</term>
<term d5p1:id="W08004">Test2</term>
</group>
<group d5p1:id="W1A0" d5p1:index="0" d5p1:key="" d5p1:name="UC RPB">
<term d5p1:id="W1A003">000000000000000022</term>
<term d5p1:id="W1A081">2,2 %</term>
<term d5p1:id="W1A082">2,18839</term>
</group>
</report>
</reports>
</xmlReply>
</uc>
</Content>
</Individual>';
WITH XMLNAMESPACES('http://www.uc.se/schemas/ucOrderReply/' AS ns,'http://www.uc.se/schemas/ucOrderReply/' AS d5p1)
SELECT X.g.value('(@d5p1:id)','varchar(20)') AS id,
X.g.value('(text())[1]','varchar(20)') AS term
FROM @XML.nodes('Individual/Content/ns:uc/ns:xmlReply/ns:reports/ns:report/ns:group/ns:term') X(g);
我注意到 XML 自从我用来编写此答案的初始版本以来已经更改。此答案尚未(读作“不会”)为此进行调整。