.net XPath选择仅包含一个无子节点的最上面的节点
我在.NET中寻找一个XPath,它只选择包含一个终端(无子节点)节点的最上面的节点 编辑 我想我已经接近你了.net XPath选择仅包含一个无子节点的最上面的节点,.net,xpath,count,.net,Xpath,Count,我在.NET中寻找一个XPath,它只选择包含一个终端(无子节点)节点的最上面的节点 编辑 我想我已经接近你了 .//*[count(descendant-or-self::terminal)=1] [ not(ancestor::*[count(.//terminal)=1]) ] 可通过以下方式选择终端的位置: *[not(*)] 仅仅/*[计数(后代或自身::*)=1]有什么问题 % cat uppermost.xml <R> <A><B>&l
.//*[count(descendant-or-self::terminal)=1] [ not(ancestor::*[count(.//terminal)=1]) ]
可通过以下方式选择终端的位置:
*[not(*)]
仅仅
/*[计数(后代或自身::*)=1]
有什么问题
% cat uppermost.xml
<R>
<A><B><C/></B></A>
<D>
<E><F/></E>
<G/>
<H><I><J/></I></H>
</D>
</R>
% xpquery '//*[count(descendant-or-self::*)=1]' uppermost.xml
<C/>
<F/>
<G/>
<J/>
%
%cat uppermost.xml
%xpquery'/*[count(后代或自身::*)=1]'uppermost.xml
%
我自己的解决方案:
.//*[count(descendant-or-self::terminal)=1] [ not(ancestor::*[count(descendant::terminal)=1]) ]
其中终端=*[非(*)]
使用第一个条件
[count(后代或self::terminal)=1]
我们选择,看起来您获得的是无子女节点。我想选择{aeg H},有点像第一位祖父母,他的后代都是独生子女。我想指出独生子女策略开始的地方。嗯,我认为没有递归(节点是向上移动1步还是2步(或更多步?)是不容易做到的;可能更容易选择无子节点,然后使用其他代码在父节点
轴上遍历它们,直到满足其他节点(或根元素)条件。是的,您是对的。最简单的方法似乎是通过递归方法向上移动这些无子节点。然而,我更感兴趣,并且在思考XPath表达式时遇到了更多的麻烦。
% cat uppermost.xml
<R>
<A><B><C/></B></A>
<D>
<E><F/></E>
<G/>
<H><I><J/></I></H>
</D>
</R>
% xpquery '//*[count(descendant-or-self::*)=1]' uppermost.xml
<C/>
<F/>
<G/>
<J/>
%
.//*[count(descendant-or-self::terminal)=1] [ not(ancestor::*[count(descendant::terminal)=1]) ]
A <-- <··· A
B <--
C <--
D <···
E <-- <··· E
F <--
G <-- <··· G
H <-- <··· H
I <--
J <--
Public Sub XmlTrimOneChildParents(ByRef xE As XmlElement)
Dim xNodeList As XmlNodeList = xE.SelectNodes("./*")
Select Case xNodeList.Count
Case 0 'terminal
Case 1 'Remove And Promote Child
Dim xChild As XmlNode = xNodeList(0)
XmlTrimOneChildParents(xChild)
xE.ParentNode.InsertBefore(xChild, xE)
xE.ParentNode.RemoveChild(xE)
Case else
For Each xChild As XmlNode In xNodeList
XmlTrimOneChildParents(xChild)
Next
End Select
End Sub