問題描述
我正在嘗試識別樹中通向特定節(jié)點的所有節(jié)點.
我正在嘗試通過 MSSQL XML (2005) 或 ASP 經(jīng)典中的 Microsoft.XMLDOM 來實現(xiàn)這一點.
我知道 XPATH 的邏輯,但 SQL Server 不支持 ancestor-or-self
軸,而且 XMLDOM 似乎在 ::
符號上窒息..>
當我在 XPATH 測試器中測試時有效的 xpath 是
//static[@id=6]/ancestor-or-self::static
我的 XML(在 sql server 中遞歸生成)看起來像
<static id="1" title="some title 1"/><static id="2" title="一些標題 2"><兒童><static id="3" title="some title 3"/><static id="4" title="some title 4"><兒童><static id="5" title="some title 5"/><static id="6" title="some title 6"/></兒童></靜態(tài)></兒童></靜態(tài)><static id="7" title="some title 7"/></root>
XPATH 應(yīng)該以任何順序選擇 id 為 (2,4,6) 的節(jié)點,因此我可以為所有節(jié)點添加一個屬性..
這是一個菜單系統(tǒng),其中我只知道選定的葉子,并且需要將通向它的所有節(jié)點標記為 hilited..
如果您能在克服 XMLDOM 阻塞方面得到任何幫助,我將不勝感激(運行 xml.documentElement.selectNodes("http://static[@id=6]/ancestor-or-self::static")
產(chǎn)生以下錯誤:Expected token 'eof' found ':'.//static[@id=6]/ancestor-or-self-->:<--:static
)
或者尋找替代解決方案.也許在任何深度找到包含特定節(jié)點(id = 6)的所有節(jié)點..
在 W2K3 上運行,使用 IIS6 我測試了 MSXML2.XMLDomDocument.4.0 版本.
Dim XMLDom ''# As MSXML2.DOMDocument40Set XMLDom = CreateObject("MSXML2.DOMDocument.4.0")調(diào)用 XMLDom.setProperty("SelectionLanguage", "XPath")調(diào)用 XMLDom.loadXML( {文檔如上所述 - 原始 xml 文檔中的錯誤))Dim originalQuery ''# As StringoriginalQuery = "http://static[@id=6]/ancestor-or-self::static"Dim replacementQuery ''# As StringreplaceQuery = "http://static[descendant::static[@id=6] or @id=6]"Dim XmlElemList ''# As MSXML2.IXMLDOMNodeList設(shè)置 XmlElemList = XMLDom.documentElement.selectNodes(originalQuery)Dim XmlElemList2 ''# As MSXML2.IXMLDOMNodeList設(shè)置 XmlElemList2 = XMLDom.documentElement.selectNodes(replacementQuery)Dim XmlElem ''# 作為 MSXML2.IXMLDOMElementCall Response.Write("Using original query : '" & originalQuery & "' (" & XmlElemList.Length & ")<br>")對于 XmlElemList 中的每個 XmlElem調(diào)用 Response.Write("XmlEntry : " & XmlElem.getAttribute("id") & "
")調(diào)用 Response.Write("****
")下一個Call Response.Write("使用替換查詢:'" & replacementQuery & "' (" & XmlElemList2.Length & ")<br>")對于 XmlElemList2 中的每個 XmlElem調(diào)用 Response.Write("XmlEntry : " & XmlElem.getAttribute("id") & "
")調(diào)用 Response.Write("****
")下一個
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("http://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..
Running on W2K3, using IIS6 i tested the MSXML2.XMLDomDocument.4.0 version.
Dim XMLDom ''# As MSXML2.DOMDocument40
Set XMLDom = CreateObject("MSXML2.DOMDocument.4.0")
Call XMLDom.setProperty("SelectionLanguage", "XPath")
Call XMLDom.loadXML( {document as described above - mistakes in original xml doc)
)
Dim originalQuery ''# As String
originalQuery = "http://static[@id=6]/ancestor-or-self::static"
Dim replacementQuery ''# As String
replacementQuery = "http://static[descendant::static[@id=6] or @id=6]"
Dim XmlElemList ''# As MSXML2.IXMLDOMNodeList
Set XmlElemList = XMLDom.documentElement.selectNodes(originalQuery)
Dim XmlElemList2 ''# As MSXML2.IXMLDOMNodeList
Set XmlElemList2 = XMLDom.documentElement.selectNodes(replacementQuery)
Dim XmlElem ''# As MSXML2.IXMLDOMElement
Call Response.Write("Using original query : '" & originalQuery & "' (" & XmlElemList.Length & ")<br>")
For Each XmlElem In XmlElemList
Call Response.Write("XmlEntry : " & XmlElem.getAttribute("id") & "<br>")
Call Response.Write("****<br>")
Next
Call Response.Write("Using replacement query : '" & replacementQuery & "' (" & XmlElemList2.Length & ")<br>")
For Each XmlElem In XmlElemList2
Call Response.Write("XmlEntry : " & XmlElem.getAttribute("id") & "<br>")
Call Response.Write("****<br>")
Next
這篇關(guān)于替代祖先或自我(或選擇樹中具有特定子節(jié)點的所有節(jié)點)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!