2016-10-08 1 views
1

J'ai un document XML, qui contient les informations d'exécution de mon application. Je veux obtenir des éléments xml spécifiques basés sur la condition donnée.Comment obtenir un élément spécifique dans XML contenant un espace de nom par défaut basé sur la condition en utilisant XPath

Ex:
document 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> 

Au-dessus XML contient une liste des éléments <entry> qui a des informations de chaque étape d'exécution (élément <entry></entry> contient des informations d'exécution).

Supposons que si vous voulez obtenir tous "entrée" éléments qui ont des rc = 0.

Ex:

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

Pour cela, j'utilise requête XPath (ci-dessous), mais je ne peux rien obtenir.

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

Est-ce la bonne façon d'analyser XML, quelqu'un peut-il vous aider?

Répondre

1

Votre XML a namespace par défaut:

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

Notez que l'élément hérite sans préfixe espace de noms par défaut de l'ancêtre, implicitement. Vous devez mapper un préfixe, dire d, à l'espace de nom URI par défaut, et utiliser ce préfixe à l'élément de référence dans l'espace de noms par défaut:

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

xpathtester demo

+0

En utilisant ci-dessus XPath, je peux capable d'obtenir seulement "" élément. Mais je veux obtenir l'élément "entry" où le statut de sortie! = 0. –

+0

En utilisant XPath ci-dessus, je peux obtenir seulement ** ** élément. Mais je veux obtenir l'élément "entry" où ** exit status! = = 0 **. –

+0

Dans ce cas, une petite modification de la XPath ci-dessus fera: '/ d: feed/d: entrée [sop: status/@ rc! = '0']' – har07