首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用BASH中的xmlstarlet计算XML文档中的元素数?

如何使用BASH中的xmlstarlet计算XML文档中的元素数?
EN

Stack Overflow用户
提问于 2016-02-27 11:43:42
回答 1查看 2.5K关注 0票数 4

我需要计算一个元素在XML文档中发生的次数。我需要计算的元素称为“ThreadGroup”

要计数的元素:

代码语言:javascript
复制
<ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="Thread Group" enabled="true">

在下面的XML中有三个ThreadGroup元素。我们怎么才能用xmlstarlet来统计它们呢?

测试XML

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<jmeterTestPlan version="1.2" properties="2.8" jmeter="2.13 r1665067">
<hashTree>
<TestPlan guiclass="TestPlanGui" testclass="TestPlan" testname="Test Plan" enabled="true">
  <stringProp name="TestPlan.comments"></stringProp>
  <boolProp name="TestPlan.functional_mode">false</boolProp>
  <boolProp name="TestPlan.serialize_threadgroups">false</boolProp>
  <elementProp name="TestPlan.user_defined_variables" elementType="Arguments" guiclass="ArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true">
    <collectionProp name="Arguments.arguments"/>
  </elementProp>
  <stringProp name="TestPlan.user_define_classpath"></stringProp>
</TestPlan>
<hashTree>
  <ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="Thread Group" enabled="true">
    <stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
  </ThreadGroup>
  <hashTree/>
  <ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="Thread Group" enabled="true">
    <stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
  </ThreadGroup>
  <hashTree/>
  <ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="Thread Group" enabled="true">
    <stringProp name="ThreadGroup.on_sample_error">continue</stringProp>

  </ThreadGroup>
  <hashTree/>
</hashTree>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-27 11:56:38

尝试使用count()函数,例如:

代码语言:javascript
复制
xmlstarlet sel -t -c "count(//ThreadGroup)" xmlfile

在格式良好的xml文件(不是您的情况)中,它会产生以下结果:

代码语言:javascript
复制
3
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35669280

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档