在SQL Server中使用冒号解析XML值

时间:2018-04-02 14:23:23

标签: sql-server xml xquery xquery-sql

我需要帮助来解析SQL Server中的XML。我需要得到" d1p1:Val2" " d2p1:string"。

的值和值的连接
<FirstData xmlns:i="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:d1p1="http://XXXXXX" xmlns="http://YYYYYY" i:type="d1p1:StaticInfo">
<Timestamp>0</Timestamp>
<ActionResult i:nil="true" />
<d1p1:Val1 xmlns:d2p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<d2p1:string>1</d2p1:string>
<d2p1:string>2</d2p1:string>
<d2p1:string>3</d2p1:string>
<d2p1:string>4</d2p1:string>
</d1p1:Val1>
<d1p1:Val2>false</d1p1:Val2>
</FirstData>

2 个答案:

答案 0 :(得分:0)

尝试这样的事情(不能自己测试):

SELECT Instructions.query('
     declare namespace d1p1="http://XXXXXX";
     declare namespace d2p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays";
        concat(//d1p1:Val2, " ", //d2p1:string[1]);
    ')

我想你只需要详细说明这个

答案 1 :(得分:0)

您的问题不是很明确,但这可能会对您有所帮助:

DECLARE @xml XML=
N'<FirstData xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:d1p1="http://XXXXXX" xmlns="http://YYYYYY" i:type="d1p1:StaticInfo">
  <Timestamp>0</Timestamp>
  <ActionResult i:nil="true" />
  <d1p1:Val1 xmlns:d2p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
    <d2p1:string>1</d2p1:string>
    <d2p1:string>2</d2p1:string>
    <d2p1:string>3</d2p1:string>
    <d2p1:string>4</d2p1:string>
  </d1p1:Val1>
  <d1p1:Val2>false</d1p1:Val2>
</FirstData>';

WITH XMLNAMESPACES(DEFAULT 'http://YYYYYY'
                          ,'http://XXXXXX' AS d1p1
                          ,'http://schemas.microsoft.com/2003/10/Serialization/Arrays' AS d2p1)
SELECT @xml.value(N'(/FirstData/d1p1:Val2/text())[1]','bit') AS D1P1_Val2
      ,@xml.query(N'data(/FirstData/d1p1:Val1/d2p1:string/text())').value(N'text()[1]',N'nvarchar(max)') AS AllStrings;

结果

D1P1_Val2   AllStrings
0           1 2 3 4

这是 - 不推荐 - 最小查询:

SELECT @xml.value(N'(//*:Val2)[1]','bit') AS D1P1_Val2
      ,@xml.query(N'data(//*:string)').value(N'.',N'nvarchar(max)') AS AllStrings;