2016-02-27 52 views
1

我需要计算一个元素在XML文档中出现的次数。我需要计算的元素被称为“线程组”如何在BASH中使用xmlstarlet来计算XML文档中元素的数量?

Elelement数:

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

有下面的XML 3个线程组元素。我们如何使用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> 

回答

1

尝试用count()功能,如:

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

在一个结构良好的xml文件(不是你的情况下),它会产生:

3 
相关问题