Xquery 获取位置()
Xquery Get Position()
这是我们 xml 文件的一部分。
<point distanceTotal="162" seqNo="189">
<lineSection id="395" track="1" direction="1">
<outInfos>
<comment commentTypeId="4" priority="1"oneLiner="BOT">
<layerVPK seasonValue="S0"/>
<vectors>
<vector dateFrom="2016-12-11"/>
</vectors>
<frenchText>1x3 MH</frenchText>
</comment>
<comment commentTypeId="4" priority="1" oneLiner="bot">
<layerVPK seasonValue="S0"/>
<frenchText>Réception voie occupée</frenchText>
<dutchText>Test</dutchText>
</comment>
</outInfos>
</point>
我们正在将其上传到 SqlServer 列,并使用 XQuery 获取值。
但是,我找不到对 position() 进行编码的方法,并且基本上 T-SQL ROW_NUMBER 或密集排名不能使用,因为并非所有数据都存在。
例如,dutchText 仅存在于第二条评论中,并且没有标识这两条评论的字段....
这是SQL代码
SELECT fi.file_uid,
fi.file_date,
T1.ref.value('@id', 'varchar(100)') AS gTV_id,
T2.ref.value('@id', 'varchar(100)') AS gTrn_id,
T4.ref.value('@seqNo', 'varchar(100)') AS gTrnTPp_seqNo,
T7.ref.value('text()[1]', 'varchar(1000)') AS gTrnTPpOiCDT_Text,
T6.ref.query('/globalTrainVariant/trains/globalTrainVariant/train/timetablePoints/point/outInfos/comment[position()]') AS Test
FROM ods.filesin fi
CROSS APPLY fi.file_xml.nodes('declare namespace cern="http://...";
(/cern:trains/globalTrainVariant)') T1(ref)
CROSS APPLY T1.ref.nodes('declare namespace cern="http://...";
(train)') T2(ref)
CROSS APPLY T2.ref.nodes('declare namespace cern="http://...";
(timetablePoints)') T3(ref)
CROSS APPLY T3.ref.nodes('declare namespace cern="http://...";
(point)') T4(ref)
CROSS APPLY T4.ref.nodes('declare namespace cern="http://...";
(outInfos)') T5(ref)
CROSS APPLY T5.ref.nodes('declare namespace cern="http://...";
(comment)') T6(ref)
CROSS APPLY T6.ref.nodes('declare namespace cern="http://...";
(dutchText)') T7(ref)
WHERE fi.file_type = 'trains'
代码没有错误,但测试字段始终为空。
有什么建议吗?
如果您查找 documentation,您会发现,到目前为止,您不能 return 直接 position()
函数的结果:
In SQL Server, fn:position() can only be used in the context of a
context-dependent predicate. Specifically, it can only be used inside
brackets ([ ]).
但是,您可以使用一个巧妙的技巧来获得它。也就是说,您可以将元素的位置与已知序列进行比较,然后 return 来自该序列的匹配值。下面的示例说明了这一点。
declare @x xml = N'<point distanceTotal="162" seqNo="189">
<outInfos>
<comment commentTypeId="4" priority="1" oneLiner="BOT">
<layerVPK seasonValue="S0" />
<vectors>
<vector dateFrom="2016-12-11" />
</vectors>
<frenchText>1x3 MH</frenchText>
</comment>
<comment commentTypeId="4" priority="1" oneLiner="bot">
<layerVPK seasonValue="S0" />
<frenchText>Réception voie occupée</frenchText>
<dutchText>Test</dutchText>
</comment>
</outInfos>
</point>';
with cte as (
select top (1000) row_number() over(order by ac.object_id) as [RN]
from sys.all_columns ac
)
select t.c.query('.') as [OutInfos], sq.RN as [TextPosition], x.c.query('.') as [DutchComment]
from @x.nodes('/point/outInfos') t(c)
cross join cte sq
cross apply t.c.nodes('./comment[position() = sql:column("sq.RN")]/dutchText') x(c);
在其中,CTE 生成一组有序的整数(我通常保留一个特殊的 table,但您可以随时构造一个),并且在 XQuery 表达式中指定匹配条件定义 x(c)
输出。
我同意 Roger 的观点,position() 函数不能直接调用,应该在 [] 中。但是,有一个解决方案不需要任何额外的表并通过使用递归支持任意数量的行:
declare @Xml xml = N'<?xml version="1.0" encoding="utf-16"?>
<root>
<n>1</n>
<n>10</n>
<n>5</n>
<n>3</n>
<n>11</n>
</root>';
with cte as
(
select t.c.value(N'n[1]', N'int') n, 1 RowNum
from @Xml.nodes(N'root[1]') t(c)
where t.c.exist(N'n[1]') = 1
union all
select t.c.value(N'n[position() = sql:column("cte.RowNum") + 1][1]', N'int') n, cte.RowNum + 1
from @Xml.nodes(N'root[1]') t(c)
cross join cte
where t.c.exist(N'n[position() = sql:column("cte.RowNum") + 1]') = 1
)
select *
from cte;
使用 Node Order Comparison Operators 来计算 XML 树中前面的 //comment 节点可能更简单且性能更好。
我没有对巨大的 XML 文档进行测试,但它肯定 I/O 强度较低,而且我的人为测试也没有 CPU 强度。
declare @x xml = N'<point distanceTotal="162" seqNo="189">
<outInfos>
<comment commentTypeId="4" priority="1" oneLiner="BOT">
<layerVPK seasonValue="S0" />
<vectors>
<vector dateFrom="2016-12-11" />
</vectors>
<frenchText>1x3 MH</frenchText>
</comment>
<comment commentTypeId="4" priority="1" oneLiner="bot">
<layerVPK seasonValue="S0" />
<frenchText>Réception voie occupée</frenchText>
<dutchText>Test</dutchText>
</comment>
</outInfos>
</point>';
select
[OutInfos] = t.c.query('../..'),
[TextPosition] = t.c.value('let $dutchText := . return count(../../comment[. << $dutchText])', 'int'),
[DutchComment] = t.c.query('.')
from @x.nodes('/point/outInfos/comment/dutchText') t(c)
这是我们 xml 文件的一部分。
<point distanceTotal="162" seqNo="189">
<lineSection id="395" track="1" direction="1">
<outInfos>
<comment commentTypeId="4" priority="1"oneLiner="BOT">
<layerVPK seasonValue="S0"/>
<vectors>
<vector dateFrom="2016-12-11"/>
</vectors>
<frenchText>1x3 MH</frenchText>
</comment>
<comment commentTypeId="4" priority="1" oneLiner="bot">
<layerVPK seasonValue="S0"/>
<frenchText>Réception voie occupée</frenchText>
<dutchText>Test</dutchText>
</comment>
</outInfos>
</point>
我们正在将其上传到 SqlServer 列,并使用 XQuery 获取值。 但是,我找不到对 position() 进行编码的方法,并且基本上 T-SQL ROW_NUMBER 或密集排名不能使用,因为并非所有数据都存在。 例如,dutchText 仅存在于第二条评论中,并且没有标识这两条评论的字段....
这是SQL代码
SELECT fi.file_uid,
fi.file_date,
T1.ref.value('@id', 'varchar(100)') AS gTV_id,
T2.ref.value('@id', 'varchar(100)') AS gTrn_id,
T4.ref.value('@seqNo', 'varchar(100)') AS gTrnTPp_seqNo,
T7.ref.value('text()[1]', 'varchar(1000)') AS gTrnTPpOiCDT_Text,
T6.ref.query('/globalTrainVariant/trains/globalTrainVariant/train/timetablePoints/point/outInfos/comment[position()]') AS Test
FROM ods.filesin fi
CROSS APPLY fi.file_xml.nodes('declare namespace cern="http://...";
(/cern:trains/globalTrainVariant)') T1(ref)
CROSS APPLY T1.ref.nodes('declare namespace cern="http://...";
(train)') T2(ref)
CROSS APPLY T2.ref.nodes('declare namespace cern="http://...";
(timetablePoints)') T3(ref)
CROSS APPLY T3.ref.nodes('declare namespace cern="http://...";
(point)') T4(ref)
CROSS APPLY T4.ref.nodes('declare namespace cern="http://...";
(outInfos)') T5(ref)
CROSS APPLY T5.ref.nodes('declare namespace cern="http://...";
(comment)') T6(ref)
CROSS APPLY T6.ref.nodes('declare namespace cern="http://...";
(dutchText)') T7(ref)
WHERE fi.file_type = 'trains'
代码没有错误,但测试字段始终为空。
有什么建议吗?
如果您查找 documentation,您会发现,到目前为止,您不能 return 直接 position()
函数的结果:
In SQL Server, fn:position() can only be used in the context of a context-dependent predicate. Specifically, it can only be used inside brackets ([ ]).
但是,您可以使用一个巧妙的技巧来获得它。也就是说,您可以将元素的位置与已知序列进行比较,然后 return 来自该序列的匹配值。下面的示例说明了这一点。
declare @x xml = N'<point distanceTotal="162" seqNo="189">
<outInfos>
<comment commentTypeId="4" priority="1" oneLiner="BOT">
<layerVPK seasonValue="S0" />
<vectors>
<vector dateFrom="2016-12-11" />
</vectors>
<frenchText>1x3 MH</frenchText>
</comment>
<comment commentTypeId="4" priority="1" oneLiner="bot">
<layerVPK seasonValue="S0" />
<frenchText>Réception voie occupée</frenchText>
<dutchText>Test</dutchText>
</comment>
</outInfos>
</point>';
with cte as (
select top (1000) row_number() over(order by ac.object_id) as [RN]
from sys.all_columns ac
)
select t.c.query('.') as [OutInfos], sq.RN as [TextPosition], x.c.query('.') as [DutchComment]
from @x.nodes('/point/outInfos') t(c)
cross join cte sq
cross apply t.c.nodes('./comment[position() = sql:column("sq.RN")]/dutchText') x(c);
在其中,CTE 生成一组有序的整数(我通常保留一个特殊的 table,但您可以随时构造一个),并且在 XQuery 表达式中指定匹配条件定义 x(c)
输出。
我同意 Roger 的观点,position() 函数不能直接调用,应该在 [] 中。但是,有一个解决方案不需要任何额外的表并通过使用递归支持任意数量的行:
declare @Xml xml = N'<?xml version="1.0" encoding="utf-16"?>
<root>
<n>1</n>
<n>10</n>
<n>5</n>
<n>3</n>
<n>11</n>
</root>';
with cte as
(
select t.c.value(N'n[1]', N'int') n, 1 RowNum
from @Xml.nodes(N'root[1]') t(c)
where t.c.exist(N'n[1]') = 1
union all
select t.c.value(N'n[position() = sql:column("cte.RowNum") + 1][1]', N'int') n, cte.RowNum + 1
from @Xml.nodes(N'root[1]') t(c)
cross join cte
where t.c.exist(N'n[position() = sql:column("cte.RowNum") + 1]') = 1
)
select *
from cte;
使用 Node Order Comparison Operators 来计算 XML 树中前面的 //comment 节点可能更简单且性能更好。
我没有对巨大的 XML 文档进行测试,但它肯定 I/O 强度较低,而且我的人为测试也没有 CPU 强度。
declare @x xml = N'<point distanceTotal="162" seqNo="189">
<outInfos>
<comment commentTypeId="4" priority="1" oneLiner="BOT">
<layerVPK seasonValue="S0" />
<vectors>
<vector dateFrom="2016-12-11" />
</vectors>
<frenchText>1x3 MH</frenchText>
</comment>
<comment commentTypeId="4" priority="1" oneLiner="bot">
<layerVPK seasonValue="S0" />
<frenchText>Réception voie occupée</frenchText>
<dutchText>Test</dutchText>
</comment>
</outInfos>
</point>';
select
[OutInfos] = t.c.query('../..'),
[TextPosition] = t.c.value('let $dutchText := . return count(../../comment[. << $dutchText])', 'int'),
[DutchComment] = t.c.query('.')
from @x.nodes('/point/outInfos/comment/dutchText') t(c)