祖先或自我的替代方案(或选择树中具有特定子节点的所有节点)
我试图识别树中通向特定节点的所有节点。
我试图通过 MSSQL XML (2005) 或 ASP classic 中的 Microsoft.XMLDOM 来完成此任务。
我知道 XPATH 的逻辑,但 SQL Server 不支持 ancestor-or-self
轴,并且 XMLDOM 似乎因 ::
符号而窒息
。我在XPATH测试器中测试它是
//static[@id=6]/ancestor-or-self::static
我的XML(在sql server中递归生成)看起来像
<root>
<static id="1" title="some title 1" />
<static id="2" title="some title 2">
<children>
<static id="3" title="some title 3" />
<static id="4" title="some title 4">
<children>
<static id="5" title="some title 5" />
<static id="6" title="some title 6" />
</children>
</static>
</children>
</static>
<static id="7" title="some title 7" />
</root>
XPATH应该以任何顺序选择id为(2,4,6)的节点,所以我可以向所有节点添加一个属性..
这是对于菜单系统,我只知道选定的叶子,并且需要将通向它的所有节点标记为 hilited..
我将不胜感激任何克服 XMLDOM 阻塞的帮助 (运行 xml.documentElement.selectNodes("//static[@id=6]/ancestor-or-self::static")
会产生以下错误:Expected token ' eof' 发现 ':'. //static[@id=6]/ancestor-or-self-->:<--:static
)
或寻找替代解决方案。也许在任何深度找到包含特定节点(id = 6)的所有节点。
I am trying to identify all the nodes in a tree that lead to a specific node.
I am trying to accomplish this through either MSSQL XML (2005) or by the Microsoft.XMLDOM in ASP classic.
I know the logic with XPATH but SQL Server does not support the ancestor-or-self
axis and XMLDOM seems to choke on the ::
notation..
The xpath that works when i test it in XPATH testers is
//static[@id=6]/ancestor-or-self::static
my XML (generated recursively in sql server) looks like
<root>
<static id="1" title="some title 1" />
<static id="2" title="some title 2">
<children>
<static id="3" title="some title 3" />
<static id="4" title="some title 4">
<children>
<static id="5" title="some title 5" />
<static id="6" title="some title 6" />
</children>
</static>
</children>
</static>
<static id="7" title="some title 7" />
</root>
the XPATH should select nodes with id (2,4,6) in any order, so i can add an attribute to all of them ..
This is for a menu system, where i only know the selected leaf, and need to mark as hilited all the nodes leading to it..
I would appreciate any assistance in either overcoming the XMLDOM choking
(running xml.documentElement.selectNodes("//static[@id=6]/ancestor-or-self::static")
produces the following error: Expected token 'eof' found ':'. //static[@id=6]/ancestor-or-self-->:<--:static
)
or with finding an alternative solution. Maybe finding all nodes that contain the specific node (with id = 6 ) at any depth..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一种“收拾残局”之类的答案。
首先,您的主要问题是“Microsoft.XMLDOM”通常会加载版本 3.0 实现 (MSXML3.dll)。 MSXML3 确实支持完整的 XPATH 1.0 语言,但默认情况下不支持。以下应该足以修复:-
Marvin 的答案在使用 MSXML4 时包含这一行,但实际上并不是必需的,因为 XPath 是 4 及更高版本上的默认选择语言。
不过,我在上面使用了“应该”这个词,这是经过深思熟虑的。我经常遇到被第三方应用程序破坏的服务器,这些应用程序也包含 MSXML2 的分发版,但安装不正确。它们导致“Microsoft.XMLDOM”和非版本特定的“MSXML2.DOMDocument”返回 MSXML2.dll 实现而不是 MSXML3 实现。
因此,我通常建议使用的最佳 ProgID 是“MSXML2.DOMDocument.3.0”,因为您确切地知道自己会得到什么。此外,MSXML3.dll 保证可以开箱即用地安装在当前支持的所有 Windows 操作系统上。当使用旧的 progID 调用 DOM 文档时,MSXML3 仍然与 MSXML2 实现中的错误兼容。使用版本特定的 ProgID 会使 MSXML3 更严格地符合 XML 标准。
This a "tidy up the loose ends" sort of answers.
First your primary problem would be that "Microsoft.XMLDOM" would normally load version 3.0 implementation (MSXML3.dll). MSXML3 does support the full XPATH 1.0 language but not by default. The follow should be enough to fix:-
Marvin's answer includes this line when using MSXML4 but it isn't really necessary since XPath is the default selection language on 4 and above.
However I use the word should above advisedly. I've often come across servers that have been compromised by a third-party application which also include a distribution of MSXML2 but install it incorrectly. They cause "Microsoft.XMLDOM" and the non version specific "MSXML2.DOMDocument" to return an MSXML2.dll implementation instead of MSXML3 implementations.
I therefore normally recommend that the best ProgID to use is "MSXML2.DOMDocument.3.0" since you know exactly what you are getting. In addition MSXML3.dll is guaranteed to be installed on all currently supported Windows OSes out of the box. Also MSXML3 remained compatible with bugs in the MSXML2 implentation when the DOM Document is invoked using an older progID. Using the version specific ProgID causes MSXML3 to conform more strictly to XML standards.
在 W2K3 上运行,使用 IIS6 我测试了 MSXML2.XMLDomDocument.4.0 版本。
Running on W2K3, using IIS6 i tested the MSXML2.XMLDomDocument.4.0 version.