gpt4 book ai didi

c# - XPath 显式索引过滤器性能

转载 作者:数据小太阳 更新时间:2023-10-29 02:13:44 26 4
gpt4 key购买 nike

我正在创建一个 xsl stylehseet 并想出了这个(在我看来是不合逻辑的行为):

这个 XPath:

/root/element[1][@attr1 != '1' or @attr2 != 'test']

比这个 XPath 慢得多:

/root/element[count(preceding-sibling::element) + 1 = 1) and (@attr1 != '1' or @attr2 != 'test')]

我有 50 个示例 xml,第一个 XPath 需要大约 55 秒。
使用第二个 XPath 需要大约 4 秒!

我使用 XslCompiledTransform (C# .NET 4.5)。

有人可以解释为什么第一个 XPath 比第二个慢得多吗?我一直认为使用显式索引过滤器更好。

更新:一些示例 xml:

<?xml version="1.0" encoding="iso-8859-1"?>
<root>
<element attr2="test" attr1="1">
<child>17</child>
<child>17</child>
<child>16</child>
...
<child>3</child>
<child>2</child>
<child>1</child>
</element>
<element attr2="test2" attr1="2">
<child/>
<child/>
<child/>
<child/>
<child/>
<child/>
<child/>
...
<child/>
</element>
....
<element attr2="test21" attr1="21" />

只有大约 20-25 个元素和 n 个子元素,但深度最大值为 4 (/root/element/child/anotherChild)。

最佳答案

我得出的解决方案是我必须接受这个。微软说https://support.microsoft.com/en-us/kb/815124 :

All versions of MSXML, Version 3.0 and later, are faster with the explicit index filter. The improvement in performance depends on the position of the element in the child list of the parent. Instead of using the following:

/child_element

use the following:

/child_element[1]

就我而言,第一个示例比 Microsoft 的推荐要快得多。

关于c# - XPath 显式索引过滤器性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30795701/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com