如何使用BASH中的xmlstarlet计算XML文档中的元素数量? [英] How can I count the number of elements in an XML document using xmlstarlet in BASH?

查看:57
本文介绍了如何使用BASH中的xmlstarlet计算XML文档中的元素数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要计算元素在XML文档中出现的次数.我需要计算的元素称为"ThreadGroup"

I need to count the number of times an element occurs within an XML document. The element I need to count is called 'ThreadGroup'

要计数的元素:

<ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="Thread Group" enabled="true">

以下XML中包含三个ThreadGroup元素.我们如何使用xmlstarlet来计算它们?

There are three ThreadGroup elements in the following XML. How can we count them using xmlstarlet?

测试XML

<?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>

推荐答案

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

xmlstarlet sel -t -c "count(//ThreadGroup)" xmlfile

在格式正确的 xml 文件(不是您的情况)中,它会产生:

In a well-formed xml file (not your case) it would yield:

3

这篇关于如何使用BASH中的xmlstarlet计算XML文档中的元素数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆