Reputation: 1749
I need to extract some info from a XML using XPath ... the XML is like the following ...
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" >
<title type="text">DataEntities</title>
<m:count>4</m:count>
<entry>
<id>0001</id>
<title type="text">DataEntities</title>
<content type="application/xml">
<m:properties>
<d:internalId>5b1daf597f3aee4f0d93e1ed</d:internalId>
<d:datasetVersion>2</d:datasetVersion>
<d:Product>PRODUCT0001</d:Product>
<d:Version>6.0.0</d:Version>
<d:Middleware>Apache WebServer</d:Middleware>
<d:Versionmiddleware>2.2.31</d:Versionmiddleware>
<d:Hostname>server01.mydomain.com</d:Hostname>
<d:Technology>PHP</d:Technology>
</m:properties>
</content>
</entry>
<entry>
<id>0002</id>
<title type="text">DataEntities</title>
<content type="application/xml">
<m:properties>
<d:internalId>5b1daf345f3aee4f0d34e1ed</d:internalId>
<d:datasetVersion>2</d:datasetVersion>
<d:Product>PRODUCT0002</d:Product>
<d:Version>1.0.0</d:Version>
<d:Middleware>Apache WebServer</d:Middleware>
<d:Versionmiddleware>2.2.31</d:Versionmiddleware>
<d:Hostname>server02.mydomain.com</d:Hostname>
<d:Technology>Java</d:Technology>
</m:properties>
</content>
</entry>
<entry>
<id>0003</id>
<title type="text">DataEntities</title>
<content type="application/xml">
<m:properties>
<d:internalId>5b1daf123f3aee4f0d33e1ed</d:internalId>
<d:datasetVersion>2</d:datasetVersion>
<d:Product>PRODUCT0003</d:Product>
<d:Version>5.0.0</d:Version>
<d:Middleware>Apache WebServer</d:Middleware>
<d:Versionmiddleware>2.2.31</d:Versionmiddleware>
<d:Hostname>server01.mydomain.com</d:Hostname>
<d:Technology>PHP</d:Technology>
</m:properties>
</content>
</entry>
<entry>
<id>0004</id>
<title type="text">DataEntities</title>
<content type="application/xml">
<m:properties>
<d:internalId>5b1daf345f3aee4f0d34e1ed</d:internalId>
<d:datasetVersion>2</d:datasetVersion>
<d:Product>PRODUCT0004</d:Product>
<d:Version>4.0.0</d:Version>
<d:Middleware>Apache WebServer</d:Middleware>
<d:Versionmiddleware>2.2.31</d:Versionmiddleware>
<d:Hostname>server01.mydomain.com</d:Hostname>
<d:Technology>PHP</d:Technology>
</m:properties>
</content>
</entry>
</feed>
What I need is to find the products that are on the same hostname, for example fixing the server like "server01.mydomain.com" to extract, as result, PRODUCT0001, PRODUCT0003 and PRODUCT0004 (note NOT PRODUCT0002), or, fixing the server like "server02.mydomain.com" to extract , as result, only PRODUCT0002.
I've tried to use
//d:Product
and it works and the result is
Element='<d:Product xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">PRODUCT0001</d:Product>'
Element='<d:Product xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">PRODUCT0002</d:Product>'
Element='<d:Product xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">PRODUCT0003</d:Product>'
Element='<d:Product xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">PRODUCT0004</d:Product>'
I don't know how to indicate that I need "only" the products that are on the d:Hostname
value "server01.mydomain.com"
I've tried using
//d:Product/@d:Hostname['server01.mydomain.com']
but it doesn't work.
Any suggestions about the XPath syntax I have to use?
Upvotes: 1
Views: 42
Reputation: 111716
Assuming that you've properly bound the namespace prefixes as declared in your XML to your XPath library, this XPath,
//m:properties[d:Hostname="server01.mydomain.com"]/d:Product
will return those d:Product
elements in m:properties
with d:Hostname
string value equal to "server01.mydomain.com"
, as requested.
Upvotes: 1