2016-10-08 65 views
1

我有一個XML文檔,其中包含我的應用程序的執行信息。我想根據給定的條件獲取特定的xml元素。如何根據使用XPath的條件獲取包含默認名稱空間的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>元素的列表。

假設,如果想要得到這些具有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的正確方法,任何人都可以幫助嗎?

回答

1

您的XML具有默認命名空間:

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

注意前綴的元素繼承祖先的默認命名空間,含蓄。你需要映射一個前綴,說d,在默認命名空間URI,並使用該前綴在默認命名空間中的元件:

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

xpathtester demo

+0

通過採用上述的XPath,我可以只能得到「」元素。但是我想獲得退出狀態!= 0的「entry」元素。 –

+0

通過使用上面的XPath,我只能得到** **元素。但我想獲得「入口」元素**退出狀態!= 0 **。 –

+0

在這種情況下,對上述XPath進行一些修改:'/ d:feed/d:entry [sop:status/@ rc!='0']' – har07

相關問題