2016-02-27 4 views
1

Мне нужно подсчитать количество раз, когда элемент возникает в XML-документе. Элемент я должен рассчитывать называется «ThreadGroup»Как я могу подсчитать количество элементов в документе XML с помощью xmlstarlet в BASH?

Elelement сосчитать:

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

Есть три ThreadGroup элементы в следующем формате XML. Как мы можем считать их с помощью xmlstarlet?

Test 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