代码之家  ›  专栏  ›  技术社区  ›  Cesare

使用xpath有条件地从命名空间xml中提取值

  •  1
  • Cesare  · 技术社区  · 7 年前

    我需要使用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语法有什么建议吗?

    1 回复  |  直到 7 年前
        1
  •  1
  •   kjhughes    7 年前

    假设你已经 properly bound the namespace prefixes 正如在XML中向xpath库声明的,这个xpath,

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

    会归还那些 d:Product 元素在 m:properties 具有 d:Hostname 字符串值等于 "server01.mydomain.com" ,根据要求。