2016-02-27 29 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 
相關問題