【发布时间】:2023-03-25 01:08:01
【问题描述】:
我们有一个内部软件可以处理松散定义的 XML 文件。我正在尝试从 T-SQL 中的此步骤中提取子节点。我能够提取父节点,但是每当我查询子节点时,我都会收到
XML 文件大致如下所示:
<?xml version="1.0"?>
<root>
<steps>
<step>
<steptypeX attribute="somevalue">
<child1>Value</child1>
<child2>Value</child2>
</steptypeX>
</step>
</steps>
</root>
我正在使用以下 T-SQL:
select
doc.col.query('/child*') --If I use '.' or '*' here I can get the children as XML, but I want the values contained within the nodes on separate rows
from @xmldoc.nodes('/root/steps/step/steptypeX') doc(col)
where doc.col.value('@attribute', 'nvarchar(max)') = 'somevalue'
我收到的错误信息不清楚:
XQuery [query()]: Syntax error near '<eof>'
据我所知,节点确实存在,并且我没有留下任何带有斜杠的 XQuery 指令。我真的不知道我在这里做错了什么。
【问题讨论】:
-
您需要从您的 xml 中删除这一行 -
<?xml version="1.0"?>。删除该行后,您的查询在我的系统中完美运行。 -
@KrishnrajRana 我刚刚尝试过,但它对我不起作用 - 无论是否使用 XML 声明,仍然会出现相同的
错误
标签: sql-server xml tsql nodes