如何统计XElement的所有标签?

时间:2016-08-25 09:51:32

标签: xpath

这是XElementDim oTaget=

<target xmlns="urn:oasis:names:tc:xliff:document:1.2">
  <mrk mtype="seg" mid="1082">■ <x id="8746" /><g id="8747">Store the product ./g></mrk>
  <g id="8747">
    <mrk mtype="seg" mid="1083">For this purpose</mrk>
  </g>
</target>

我想要计数编号mrk

 Dim sentenchild As IEnumerable(Of XElement)
 sentenchild = oTaget.XPathSelectElements(dNS + ":mrk", namespaceManager)
 Return sentenchild.Count

但结果= 1

如何统计mrk的所有代码oTaget? 谢谢大家。

2 个答案:

答案 0 :(得分:1)

您的结果计数是一个,因为您设置的msg = input("Enter a word: ") h = " " half =" " first = msg[0] second = msg[1] msg2 = "gg" length = len(msg) third = msg[2] if first not in "aeiou": if second != third: print(msg.replace(msg[1], msg[1] * 10)) elif second == third: msg2 = third * 6 msg3 = (msg.replace(msg[2], msg2)) msg4 = first + msg3[2:] print(msg4) else: half = first * 10 msg10 = msg[1:length] print((half) + msg10) 选择元素,这只会计算XPath第一个子级别中的mrk个元素。您必须递归迭代target的所有子元素。

修改

VB.NET中的递归示例请参见:http://www.dotnetperls.com/recursion-vbnet

答案 1 :(得分:1)

使用.//element模式返回上下文元素中所有元素:

sentenchild = oTaget.XPathSelectElements(".//" + dNS + ":mrk", namespaceManager)

引自XPath 1.0 spec # 2.5 Abbreviated Syntax

  

.//para选择上下文节点的para元素后代

另一种方法是使用Descendants()方法:

Dim dNS As XNamespace = "urn:oasis:names:tc:xliff:document:1.2"
sentenchild = oTaget.Descendants(dNS + "mrk")