统计节点数R,xml2

Count number of nodes R, xml2

我有一个 xml 文件,想知道使用 R 的特定节点的计数。我的 xml 如下所示。节点数应该是 4。我正在使用 xml2 包。请注意,还有另一个元素 <tag>,处于我不想计算的同一级别。

感谢您的帮助。谢谢!

<root>
  <node>
    <string>1</string>
    <string>2</string>
    <string>3</string>
    <string>4</string>
  </node>
  <node>
    <string>5</string>
    <string>6</string>
    <string>7</string>
    <string>8</string>
  </node>
  <node>
    <string>9</string>
    <string>10</string>
    <string>11</string>
    <string>12</string>
  </node>
  <node>
    <string>13</string>
    <string>14</string>
    <string>15</string>
    <string>16</string>
  </node>
  <tag>
    <string>17</string>
  </tag>
</root>


library(xml2)

length(xml2::xml_find_all(doc, ".//node"))
# [1] 4

doc <- read_xml("<root>
  <node>
    <string>1</string>
    <string>2</string>
    <string>3</string>
    <string>4</string>
  </node>
  <node>
    <string>5</string>
    <string>6</string>
    <string>7</string>
    <string>8</string>
  </node>
  <node>
    <string>9</string>
    <string>10</string>
    <string>11</string>
    <string>12</string>
  </node>
  <node>
    <string>13</string>
    <string>14</string>
    <string>15</string>
    <string>16</string>
  </node>
  <tag>
    <string>17</string>
  </tag>
</root>
")