Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在SQL Server中的嵌套XML中选择一个元素_Sql_Sql Server_Xml_Xpath - Fatal编程技术网

在SQL Server中的嵌套XML中选择一个元素

在SQL Server中的嵌套XML中选择一个元素,sql,sql-server,xml,xpath,Sql,Sql Server,Xml,Xpath,我在SQLServer表中有一个包含XML内容的列 实际上,几个不同的XML被合并在这个特定的XML列中 当我这样做的时候 select columnwithXML.query('(/root/secondlayer/node())').value('(/*)[11]', 'varchar(max)') 我实际上得到了我真正需要的一个XML,它从以下内容开始: <?xml version="1.0" encoding="utf-16"?> <thirdlayer xm

我在SQLServer表中有一个包含XML内容的列

实际上,几个不同的XML被合并在这个特定的XML列中

当我这样做的时候

select
    columnwithXML.query('(/root/secondlayer/node())').value('(/*)[11]', 'varchar(max)')
我实际上得到了我真正需要的一个XML,它从以下内容开始:

<?xml version="1.0" encoding="utf-16"?>
<thirdlayer xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <someVariable1>false</someVariable1>
    <someVariable2>false</someVariable2>

假的
假的
顺便说一下:在这个
级别上有很多兄弟姐妹。我只对这个感兴趣。我现在用[11]达到了这个目标

我想读一些变量2的内容。我尝试了很多不同的选项,但大多数都返回NULL。我不太确定如何构建XML路径,尤其是因为

如果有人提出建议,我们将不胜感激

先谢谢你

添加:XML的一部分。我把它的一部分拿出来以便阅读

IYEJaxxxxxxxx
(...)
(...)
票
(...)
(...)
配置
假的
假的

我已经能够达到我只有的部分:


假的
假的


然后我就被卡住了。

在SQL Server中,您的xml不能存储为xml(由于内部放置了
text/xmldecl
元素(
)。一般来说,这是不允许的

我不知道你是怎么做到的

我已经能够到达我只有

你是用字符串方法做的吗

谁在创建这些XML?如果这在您的控制之下,您应该在没有声明的情况下执行此操作。或者(不推荐)您可以将所有嵌入的XML包含在
CDATA
中,并将其作为字符串读取(性能差,因为您必须将其转换为XML)

现有XML的解决方案 您可以在字符串级别进行黑客攻击,就像我在这里所做的那样:

DECLARE @YourXML NVARCHAR(MAX)=
N'<Configurations><Config><Label>IYE</Label><Value>Ja</Value><Description>xxxx</Description></Config><Config><Label>xxxx</Label><Value><?xml version="1.0" encoding="utf-16"?>
<ArrayOfxxxx xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <ProductOption>
    (...)
  </ProductOption>
  <ProductOption>
    (...)
  </ProductOption>
</ArrayOfxxxx></Value><Description/>
</Config><Config><Label>TICKET</Label><Value><?xml version="1.0" encoding="utf-8"?>
<Ticket xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Sections>
        (...)
    </Sections>
    <PriceSections>
     (...)
    </PriceSections>
    </Ticket></Value><Description/></Config><Config><Label>CONFIGURATION</Label><Value><?xml version="1.0" encoding="utf-16"?>
<ConfigurationData xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <VariableX>false</VariableX>
  <VariableY>false</VariableY>
</ConfigurationData></Value><Description/></Config></Configurations>';

我是否正确地理解了这一点:
/root/secondlayer
的内容又是一个完整的XML?让我感到奇怪的是
。在SQL Server中,这一点从未显示过。如果我是对的,我假设,内部存在的XML(您应该将其理解为
*N*varchar
btw)必须转换为XML才能深入到其中。这将有助于展示XML的简化示例。是的,这是正确的。XML再次全面发展。嗨,塔玛拉,这个问题解决了吗?你需要进一步的帮助吗?是的,非常感谢!我确实需要调整一些,主要是因为在真实示例中,我混合了和和。我rongly认为文件复制粘贴到stackoverflow网站会导致这种更改,因此为了更好地阅读,我手动将文件更改为<和>。但后来它成功了。虽然性能很差,但这是有意义的…再次感谢您!完成了!很抱歉,我忘了!
WITH GetMyNode(ConfigurationData) AS
(
    SELECT CAST(REPLACE(REPLACE(@YourXML,'<?','<!--?'),'?>','?-->') AS XML)
           .query('/Configurations/Config[(Label/text())[1]="CONFIGURATION"]/Value/ConfigurationData')
)
--read the CTEs XML and retrieve the two Variables
SELECT ConfigurationData.value('(ConfigurationData/VariableX/text())[1]','nvarchar(max)') AS VariableX
      ,ConfigurationData.value('(ConfigurationData/VariableY/text())[1]','nvarchar(max)') AS VariableY
FROM GetMyNode