首页 > 解决方案 > 使用 XPath 从命名空间 XML 有条件地提取值

问题描述

我需要使用 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>'

我不知道如何表明我需要“仅”d:Hostname价值为“server01.mydomain.com”的产品

我试过使用

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

但它不起作用。

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

标签: xmlxpathxml-namespaces

解决方案


假设您已将XML 中声明的命名空间前缀正确绑定到 XPath 库,这个 XPath,

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

将根据要求返回字符串值等于的那些d:Product元素。m:propertiesd:Hostname"server01.mydomain.com"


推荐阅读