如何使用xml starlet选择xml值

时间:2016-06-13 15:27:12

标签: xml windows batch-file xpath xmlstarlet

如何使用xmlstarlet检索值?

我试图从以下xml文件中检索初始值4.7:

<?xml version="1.0"?>
<GCContextualMethod xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.agilent.com/schemas/Analytical/Instrument/GC7890/2006/">
  <method xmlns="">
    <GC PostRunHoldTime="0" AutoPrepRun="UNKNOWN">
      <Inlet DeterminesReadiness="true">
        ...
      </Inlet>
      <Inlet DeterminesReadiness="true" />
      <Column DeterminesReadiness="true">
        ...
      </Column>
      <Column DeterminesReadiness="true">
        <Setpoints Mode="CONSTANT_FLOW">
          <Flow State="ON" InitialHoldTime="0" InitialValue="4.7" PostRunValue="4.7" />
        </Setpoints>
      </Column>
      <Column DeterminesReadiness="false">  

我已经尝试了以下但是它没有工作:

%cd%\resources\XML.EXE sel -t -c "/GCContextualMethod/method[@xmlns=""]/GC[@PostRunHoldTime="0"]/Column[2]/Setpoints[@Mode="CONSTANT_FLOW"]/Flow[@State="ON"]@InitialValue"  %cd%\GC78901.RapidControl.MethodXML.xml

1 个答案:

答案 0 :(得分:2)

文档元素GCContextualMethod具有名称空间uri http://www.agilent.com/schemas/Analytical/Instrument/GC7890/2006/

您需要声明该URI并在文档元素的位置步骤中使用声明的前缀。此外,不需要过滤器[@xmlns=""]

XML.EXE sel 
    -N gc=http://www.agilent.com/schemas/Analytical/Instrument/GC7890/2006/
    -t -c
    "/gc:GCContextualMethod/method/....."  
    %cd%\GC78901.RapidControl.MethodXML.xml

要测试XPath表达式,最好在运行表达式的部分时查看返回的内容。