我正在尝试调用Jenkins XML API来从其REST端点检索特定的信息集(...
表示内容中有类似的同级元素,但为简洁起见被排除在外):
<workflowRun _class="org.jenkinsci.plugins.workflow.job.WorkflowRun">
<action _class="hudson.model.CauseAction">
<cause _class="hudson.triggers.TimerTrigger$TimerTriggerCause">
<shortDescription>Started by timer</shortDescription>
</cause>
</action>
<action _class="hudson.model.ParametersAction">
<parameter _class="hudson.model.StringParameterValue">
<name>projectName</name>
<value>My-Proj</value>
</parameter>
<parameter _class="hudson.model.StringParameterValue">
<name>NodeParam</name>
<value>UY-DO1</value>
</parameter>
...
</action>
<action/>
<action/>
<action/>
<action/>
<action _class="org.jenkinsci.plugins.workflow.cps.EnvActionImpl"/>
<action/>
<action _class="hudson.tasks.junit.TestResultAction">
<failCount>21</failCount>
<skipCount>6</skipCount>
<totalCount>223</totalCount>
<urlName>myReport</urlName>
</action>
<action/>
<action/>
<action/>
<action _class="org.jenkinsci.plugins.workflow.job.views.FlowGraphAction"/>
<action/>
<action/>
<building>false</building>
...
<result>FAILURE</result>
<timestamp>1553145960340</timestamp>
...
</workflowRun>
我只想获取以下信息:
<action _class="hudson.tasks.junit.TestResultAction">
<failCount>21</failCount>
<skipCount>6</skipCount>
<totalCount>223</totalCount>
<urlName>myReport</urlName>
</action>
<timestamp>1553145960340</timestamp>
但是,我找不到一次即可捕获所有这些数据的XPath命令。我可以运行两个单独的调用来检索所需的信息,但这需要两次单独的服务器行程。我想知道是否有一个XPath命令可用来一次性检索目标<action>
和<timestamp>
:
https://jenkins_host/job/MyApp/job/Some_Jenkins_Job/lastCompletedBuild/api/xml?xpath=/workflowRun/action[@_class='hudson.tasks.junit.TestResultAction']
哪个让我们:
<action _class="hudson.tasks.junit.TestResultAction">
<failCount>21</failCount>
<skipCount>6</skipCount>
<totalCount>223</totalCount>
<urlName>myReport</urlName>
</action>
和时间戳(请注意,从xpath
切换到tree
):
https://jenkins_host/job/MyApp/job/Some_Jenkins_Job/lastCompletedBuild/api/xml?tree=timestamp
结果如下:
<workflowRun _class="org.jenkinsci.plugins.workflow.job.WorkflowRun">
<timestamp>1553145960340</timestamp>
</workflowRun>
我尝试使用tree
和xpath
的组合,但出现错误:
https://jenkins_host/job/MyApp/job/Some_Jenkins_Job/lastCompletedBuild/api/xml?tree=timestamp&xpath=/workflowRun/action[@_class='hudson.tasks.junit.TestResultAction']
似乎无法将它们混淆。我什至尝试使用它都无济于事:
xml?xpath=/workflowRun/action[@_class='hudson.tasks.junit.TestResultAction']&/workflowRun/timestamp
或
xml?xpath=/workflowRun/action[@_class='hudson.tasks.junit.TestResultAction']&xpath=/workflowRun/timestamp
您知道我如何通过一个XPath获得两组数据吗?
答案 0 :(得分:1)
下一个是一个完全有效的XPath 1.0表达式,但是我不知道您的REST端点是否可以解析它...
/workflowRun
/action[@_class='hudson.tasks.junit.TestResultAction']
|/workflowRun
/action[@_class='hudson.tasks.junit.TestResultAction']
/following-sibling::timestamp[1]
或者不进行联合(强制使用Kaysian方法):
/workflowRun
/*[self::action/@_class='hudson.tasks.junit.TestResultAction' or
self::timestamp[
count(.|../action[@_class='hudson.tasks.junit.TestResultAction']/following-sibling::timestamp[1])
= count(../action[@_class='hudson.tasks.junit.TestResultAction']/following-sibling::timestamp[1])
]]
两者中的关键是选择参考元素(action
),然后选择同级timestamp
,而不仅仅是每个 timestamp
。
答案 1 :(得分:0)
作为一种选择,可以通过|
或or
运算符之一对表达式进行联合。选择以下解决方案之一:
|
运算符: (//action[@_class='hudson.tasks.junit.TestResultAction']/descendant-or-self::*)|//timestamp
or
-运算符:
//*[self::action[@_class='hudson.tasks.junit.TestResultAction'] or self::timestamp]/descendant-or-self::*