使用 XPath 从命名空间 XML 中有条件地提取值

Extract values conditionally from namespaced XML using XPath

我需要使用 XPath 从 XML 中提取一些信息...XML 如下所示...

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

我需要的是找到相同主机名的产品, 例如固定服务器 "server01.mydomain.com" 以提取结果,PRODUCT0001、PRODUCT0003和 PRODUCT0004(注意不是 PRODUCT0002),或者,像 "server02.mydomain.com" 这样修复服务器以提取 ,结果,只有 PRODUCT0002。

我试过

//d:Product

它起作用了,结果是

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

我不知道如何表明我需要 "only" 价值 d:Hostname 的产品 "server01.mydomain.com"

我试过使用

//d:Product/@d:Hostname['server01.mydomain.com']

但是没用。

关于我必须使用的 XPath 语法有什么建议吗?

假设您已经 在您的 XML 中声明到您的 XPath 库,这个 XPath,

//m:properties[d:Hostname="server01.mydomain.com"]/d:Product

将 return m:propertiesd:Hostname 字符串值等于 "server01.mydomain.com" 的那些 d:Product 元素。