BASH,xmlstarlet如何检索XML中的值

时间:2016-02-27 15:46:02

标签: bash xmlstarlet

将xmlstarlet与BASH一起使用,我需要捕获Argument.name = Threads元素Argument.value中存储的数字。 - 谢谢你的帮助。

<stringProp name="Argument.name">Threads</stringProp>
 <stringProp name="Argument.value">99</stringProp>

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>
  <Arguments guiclass="ArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true">
    <collectionProp name="Arguments.arguments">
      <elementProp name="Threads" elementType="Argument">
        <stringProp name="Argument.name">Threads</stringProp>
        <stringProp name="Argument.value">99</stringProp>
        <stringProp name="Argument.desc">test1</stringProp>
        <stringProp name="Argument.metadata">=</stringProp>
      </elementProp>
    </collectionProp>
  </Arguments>
  <hashTree/>
</hashTree>

1 个答案:

答案 0 :(得分:1)

修复XML后,它就成了一个XPath问题。其中任何一个都应该有效:

xmlstarlet sel -t -v '//stringProp[@name="Argument.value" and ../stringProp[@name="Argument.name"] = "Threads"]' file.xml
xmlstarlet sel -t -v '//stringProp[@name="Argument.name" = "Threads"]/../stringProp[@name="Argument.value"]' file.xml
xmlstarlet sel -t -v '//elementProp[./stringProp[@name="Argument.name"] = "Threads"]/stringProp[@name="Argument.value"]' file.xml