如何使用 XPath 根据条件获取包含默认命名空间的 XML 中的特定元素

How to get specific element in XML containg default namespace based on condition using XPath

我有一个 XML 文档,其中包含我的应用程序的执行信息。我想根据给定的条件获取特定的 xml 元素。

例如:
XML 文档 :

<feed xmlns='http://www.w3.org/2005/Atom' xmlns:sop='http://www.hp.com/dma/api/sop'>
<id>https://6.152.112.7:8443/dma/api/auto/running/workflow/8618f087562b59f001579e0296227b61</id>
<author><name>HP DMA</name></author>
<updated>2016-10-07T07:19:12Z</updated>
<title>H_ARG_C_02200_Patch_MSSQl_Instances</title>
<link rel='self' type='application/atom+xml' href='https://6.152.112.7:8443/dma/api/auto/running/workflow/8618f087562b59f001579e0296227b61' />
<link rel='alternate' type='text/html' href='https://6.152.112.7:8443/dma/sop/workflow/view/8618f087562b59f00156bc297a3d3ab9' />
<entry>
<id>https://6.152.112.7:8443/dma/api/auto/running/workflow/8618f087562b59f001579e0296227b61/step/8618f087562b59f001579e0297417b74</id>
<published>2016-10-07T07:19:12Z</published>
<updated>2016-10-07T07:20:13Z</updated>
<title>MS SQL - Parameters - Install Patch</title>
<link rel='alternate' type='application/atom+xml' href='https://6.152.112.7:8443/dma/api/auto/running/workflow/8618f087562b59f001579e0296227b61/step/8618f087562b59f001579e0297417b74' />
<link rel='alternate' type='text/html' href='https://6.152.112.7:8443/dma/sop/step/view/8618f0875360c19b015360cf31b72f4c' />
<sop:status state='Finished' rc='0'/>
</entry>
<entry>
<id>https://6.152.112.7:8443/dma/api/auto/running/workflow/8618f087562b59f001579e0296227b61/step/8618f087562b59f001579e03858e7b95</id>
<published>2016-10-07T07:20:13Z</published>
<updated>2016-10-07T07:20:26Z</updated>
<title>MS SQL - Advanced Parameters - Install Patch</title>
<link rel='alternate' type='application/atom+xml' href='https://6.152.112.7:8443/dma/api/auto/running/workflow/8618f087562b59f001579e0296227b61/step/8618f087562b59f001579e03858e7b95' />
<link rel='alternate' type='text/html' href='https://6.152.112.7:8443/dma/sop/step/view/8618f0875360c19b015360cf55ae32e4' />
<sop:status state='Finished' rc='0'/>
</entry>
<entry>
<id>https://6.152.112.7:8443/dma/api/auto/running/workflow/8618f087562b59f001579e0296227b61/step/8618f087562b59f001579e03b9e97ba2</id>
<published>2016-10-07T07:20:26Z</published>
<updated>2016-10-07T07:20:43Z</updated>
<title>Check If Download File Exists</title>
<link rel='alternate' type='application/atom+xml' href='https://6.152.112.7:8443/dma/api/auto/running/workflow/8618f087562b59f001579e0296227b61/step/8618f087562b59f001579e03b9e97ba2' />
<link rel='alternate' type='text/html' href='https://6.152.112.7:8443/dma/sop/step/view/8618f0875360c19b015360ce1ffc162d' />
<sop:status state='Finished' rc='2'/>
</entry>
<entry>
<id>https://6.152.112.7:8443/dma/api/auto/running/workflow/8618f087562b59f001579e0296227b61/step/8618f087562b59f001579e03fd0c7c2b</id>
<published>2016-10-07T07:20:44Z</published>
<updated>2016-10-07T07:23:38Z</updated>
<title>Download Software</title>
<link rel='alternate' type='application/atom+xml' href='https://6.152.112.7:8443/dma/api/auto/running/workflow/8618f087562b59f001579e0296227b61/step/8618f087562b59f001579e03fd0c7c2b' />
<link rel='alternate' type='text/html' href='https://6.152.112.7:8443/dma/sop/step/view/8618f087535fbe2a01535fbe3cd80002' />
<sop:status state='Finished' rc='0'/>
</entry>
<entry>
<id>https://6.152.112.7:8443/dma/api/auto/running/workflow/8618f087562b59f001579e0296227b61/step/8618f087562b59f001579e06a8887e5b</id>
<published>2016-10-07T07:23:39Z</published>
<updated>2016-10-07T07:24:00Z</updated>
<title>MSSQL Check Build of Patch File</title>
<link rel='alternate' type='application/atom+xml' href='https://6.152.112.7:8443/dma/api/auto/running/workflow/8618f087562b59f001579e0296227b61/step/8618f087562b59f001579e06a8887e5b' />
<link rel='alternate' type='text/html' href='https://6.152.112.7:8443/dma/sop/step/view/8618f0875360c19b015360cf38dd300c' />
<sop:status state='Finished' rc='1'/>
</entry>
<entry>
<id>https://6.152.112.7:8443/dma/api/auto/running/workflow/8618f087562b59f001579e0296227b61/step/8618f087562b59f001579e06fe117f22</id>
<published>2016-10-07T07:24:00Z</published>
<updated>2016-10-07T07:24:12Z</updated>
<title>Delete File</title>
<link rel='alternate' type='application/atom+xml' href='https://6.152.112.7:8443/dma/api/auto/running/workflow/8618f087562b59f001579e0296227b61/step/8618f087562b59f001579e06fe117f22' />
<link rel='alternate' type='text/html' href='https://6.152.112.7:8443/dma/sop/step/view/8618f0875360c19b015360cc72f90321' />
<sop:status state='Finished' rc='0'/>
</entry>
<entry>
<id>https://6.152.112.7:8443/dma/api/auto/running/workflow/8618f087562b59f001579e0296227b61/step/8618f087562b59f001579e072e927f2f</id>
<published>2016-10-07T07:24:13Z</published>
<updated>2016-10-07T07:24:13Z</updated>
<title>Failure</title>
<link rel='alternate' type='application/atom+xml' href='https://6.152.112.7:8443/dma/api/auto/running/workflow/8618f087562b59f001579e0296227b61/step/8618f087562b59f001579e072e927f2f' />
<link rel='alternate' type='text/html' href='https://6.152.112.7:8443/dma/sop/step/view/5e844d3e7a134731a64fbcba727b673f' />
<sop:status state='Finished' rc='0'/>
</entry>
<sop:target server='Win8R2MSSQL-1' instance='MSSQLSERVER' />
<sop:script-execution url='https://6.152.112.7:8443/dma/api/auto/running/workflow/8618f087562b59f001579e0296227b61/scriptExecution/8618f087562b59f001579e0298de7b91'/>
<sop:status state='Failed'/>
<sop:deployment name='H_ARG_C_02200_Patch_MSSQL_Instances_Deployment'/>
</feed>

上方 XML 包含 <entry> 元素的列表,其中包含每个执行步骤的一些信息(<entry></entry> 元素包含执行信息)。

假设如果想要获取所有 "entry" 具有 rc != 0 的元素。

例如:

<sop:status state='Finished' rc='1'/>
<sop:status state='Finished' rc='2'/>

为此,我正在使用 xPath 查询(如下所示),但我什么也得不到。

/feed/entry/sop:status[@ rc!="0"]
and
//entry/sop:status[@ rc!="0"]

这是解析的正确方法吗xml,有人可以帮忙吗?

您的 XML 具有默认命名空间:

xmlns="http://www.w3.org/2005/Atom"

请注意,未加前缀的元素隐式地从祖先继承了默认命名空间。您需要将前缀(例如 d)映射到默认命名空间 URI,并使用该前缀来引用默认命名空间中的元素:

/d:feed/d:entry/sop:status[@rc!='0']

xpathtester demo