Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.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 T-SQL XML:<;eof>;尝试通过通配符查询子节点时遇到_Sql Server_Xml_Tsql_Nodes - Fatal编程技术网

Sql server T-SQL XML:<;eof>;尝试通过通配符查询子节点时遇到

Sql server T-SQL XML:<;eof>;尝试通过通配符查询子节点时遇到,sql-server,xml,tsql,nodes,Sql Server,Xml,Tsql,Nodes,我们有一个内部软件,可以处理松散定义的XML文件。我试图从T-SQL中的这个步骤中提取子节点。我能够提取父节点,但每当我查询子节点时,总是会出现语法错误 XML文件大致如下所示: <?xml version="1.0"?> <root> <steps> <step> <steptypeX attribute="somevalue"> <child1>Value</child1&g

我们有一个内部软件,可以处理松散定义的XML文件。我试图从T-SQL中的这个步骤中提取子节点。我能够提取父节点,但每当我查询子节点时,总是会出现语法错误

XML文件大致如下所示:

<?xml version="1.0"?>
<root>
  <steps>
    <step>
      <steptypeX attribute="somevalue">
        <child1>Value</child1>
        <child2>Value</child2>
      </steptypeX>
    </step>
  </steps>
</root>  
我收到的错误消息不清楚:

XQuery [query()]: Syntax error near '<eof>'  
XQuery[query()]:靠近“”的语法错误

据我所知,节点确实存在,我没有留下任何带有斜杠的XQuery指令。我真的不知道我做错了什么。

如果我正确理解您的意图,您可以使用
child:*

DECLARE @xmldoc XML =
N'<?xml version="1.0"?>
<root>
  <steps>
    <step>
      <steptypeX attribute="somevalue">
        <child1>Value</child1>
        <child2>Value</child2>
      </steptypeX>
    </step>
  </steps>
  </root>';

SELECT
   doc.col.value('text()[1]', 'nvarchar(max)')
FROM @xmldoc.nodes('/root/steps/step/steptypeX/child::*') doc(col)
WHERE doc.col.value('../@attribute', 'nvarchar(max)') = 'somevalue';
DECLARE@xmldocxml=
不
价值
价值
';
挑选
doc.col.value('text()[1]','nvarchar(max)'
从@xmldoc.nodes('/root/steps/step/steptypeX/child::*')文档(列)
其中doc.col.value(“../@attribute”,“nvarchar(max)”)=“somevalue”;

您需要从xml中删除此行-
。一旦我删除了那一行,您的查询在我的系统中就可以正常工作了。@Krishnajrana我刚刚尝试了这一点,但对我来说不起作用-无论是否使用XML声明,仍然会出现相同的错误,这似乎是我想要的!我真的不明白为什么我必须在nodes()函数中使用/child::指令并返回WHERE子句,而不是查找父节点并选择子节点though@Gargravarr关键是
/root/steps/step/steptypeX/
中的子元素具有不同的名称(child1和child2)。使用
child::*
可以选择它们。但在这种情况下,层次结构更深一步,所以需要返回到父节点以验证属性值。
DECLARE @xmldoc XML =
N'<?xml version="1.0"?>
<root>
  <steps>
    <step>
      <steptypeX attribute="somevalue">
        <child1>Value</child1>
        <child2>Value</child2>
      </steptypeX>
    </step>
  </steps>
  </root>';

SELECT
   doc.col.value('text()[1]', 'nvarchar(max)')
FROM @xmldoc.nodes('/root/steps/step/steptypeX/child::*') doc(col)
WHERE doc.col.value('../@attribute', 'nvarchar(max)') = 'somevalue';