简体   繁体   中英

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

I have an XML document, that contains execution information of my application. I want to get specific xml elements based on the given condition.

For ex:
XML document :

<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>

Above XML contains a list of <entry> elements which has some information of each execution step ( <entry></entry> element contains execution info).

Suppose if want to get all "entry" elements which are having rc != 0.

For Ex:

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

For this, I'm using xPath query(given below), but I'm not able to get anything.

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

Is this the right way to parse xml, can anyone help on this?

Your XML has default namespace :

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

Notice that unprefixed element inherits default namespace from ancestor, implicitly. You need to map a prefix, say d , to the default namespace URI, and use that prefix to reference element in the default namespace :

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

xpathtester demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM