2013-11-21 69 views
0

另一個XPath問題。我有一個API來查詢其中包含多個嵌套命名空間。XPATH - 當內部節點名稱空間不同時,在一個節點下返回整個對象

<feed xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:media="http://search.yahoo.com/mrss/" xmlns="http://www.w3.org/2005/Atom" xmlns:pamedia="http://paimages.co.uk/pamedia.htm"> 
<title> 
    Image/video search results 
</title> 
<subtitle> 
    Images/video found containing the search string provided 
</subtitle> 
<pamedia:found> 
    2228 
</pamedia:found> 
<pamedia:perpage> 
    100 
</pamedia:perpage> 
<pamedia:page> 
    1 
</pamedia:page> 
<opensearch:totalResults> 
    2228 
</opensearch:totalResults> 
<opensearch:itemsPerpage> 
    100 
</opensearch:itemsPerpage> 
<opensearch:startIndex> 
    1 
</opensearch:startIndex> 
<id> 
    http://images.pressassociation.com/cgi/search_api/?state=search&q=miley 
</id> 
<link rel="self" href="http://images.pressassociation.com/cgi/search_api/?state=search&q=miley"></link> 
<link rel="next" href="http://images.pressassociation.com/cgi/search_api/?state=search&q=miley&offset=2"></link> 
<link rel="last" href="http://images.pressassociation.com/cgi/search_api/?state=search&q=miley&offset=23"></link> 
<updated> 
    2013-11-21T09:13:21Z 
</updated> 
<link rel="self" href="http://images.pressassociation.com/cgi/search_api/?state=search&q=miley"> 
    <updated> 
     2013-11-21T09:13:21Z 
    </updated> 
    <name> 
     Press Association Images 
    </name> 
    <email> 
     redacted 
    </email> 
</link> 
<entry> 
    <pamedia:media-type> 
     image/jpeg 
    </pamedia:media-type> 
    <pamedia:event_date> 
     2013-11-14 
    </pamedia:event_date> 
    <pamedia:urn> 
     18209006 
    </pamedia:urn> 
    <pamedia:domain> 
     2 
    </pamedia:domain> 
    <pamedia:domain_prefix> 
     PA 
    </pamedia:domain_prefix> 
    <link type="application/vnd.iptc.g2.newsitem+xml" href="http://images.pressassociation.com/meta/2.18209006.xml"></link> 
    <link rel="related" href="http://images.pressassociation.com/meta/2.18209006.html" type="text/html"></link> 
    <link rel="related" href="http://images.pressassociation.com/empicsthumbnail/vol183/block3642/18209006.jpg" type="image/jpeg"></link> 
    <media:thumbnail width="127" medium="image" height="190" url="http://images.pressassociation.com/empicsthumbnail/vol183/block3642/18209006.jpg" type="image/jpeg"></media:thumbnail> 
    <media:content expression="sample" medium="image" width="511" height="767" url="http://images.pressassociation.com/image/preview/2.18209006.jpg" type="image/jpeg"></media:content> 
    <media:copyright> 
     Associated Press 
    </media:copyright> 
    <media:content expression="full" medium="photo" width="6041" height="4024" url="http://images.pressassociation.com/image/2.18209006.jpg" type="image/jpeg"></media:content> 
    <updated> 
     2013-11-15T10:56:44Z 
    </updated> 
    <summary type="html"> 
     Fans wait for singer Miley Cyrus before the Bambi 2013 media awards in Berlin, Germany, Thursday, Nov. 14, 2013. (AP Photo/Gero Breloer) 
    </summary> 
    <rights type="html"> 
     UK picture buyers only BRO110 
    </rights> 
    <id> 
     http://images.pressassociation.com/meta/2.18209006.xml 
    </id> 
    <title type="html"> 
     2013 Bambi Media Awards - Berlin 
    </title> 
    <category term="E"></category> 
    <author> 
     <name> 
      Gero Breloer/AP 
     </name> 
    </author> 
</entry> 
<entry> 
    <pamedia:media-type> 
     image/jpeg 
    </pamedia:media-type> 
    <pamedia:event_date> 
     2013-11-14 
    </pamedia:event_date> 
    <pamedia:urn> 
     18207923 
    </pamedia:urn> 
    <pamedia:domain> 
     2 
    </pamedia:domain> 
    <pamedia:domain_prefix> 
     PA 
    </pamedia:domain_prefix> 
    <link type="application/vnd.iptc.g2.newsitem+xml" href="http://images.pressassociation.com/meta/2.18207923.xml"></link> 
    <link rel="related" href="http://images.pressassociation.com/meta/2.18207923.html" type="text/html"></link> 
    <link rel="related" href="http://images.pressassociation.com/empicsthumbnail/vol183/block3642/18207923.jpg" type="image/jpeg"></link> 
    <media:thumbnail width="127" medium="image" height="198" url="http://images.pressassociation.com/empicsthumbnail/vol183/block3642/18207923.jpg" type="image/jpeg"></media:thumbnail> 
    <media:content expression="sample" medium="image" width="511" height="796" url="http://images.pressassociation.com/image/preview/2.18207923.jpg" type="image/jpeg"></media:content> 
    <media:copyright> 
     Associated Press 
    </media:copyright> 
    <media:content expression="full" medium="photo" width="2801" height="1796" url="http://images.pressassociation.com/image/2.18207923.jpg" type="image/jpeg"></media:content> 
    <updated> 
     2013-11-15T10:56:44Z 
    </updated> 
    <summary type="html"> 
     Singer Miley Cyrus arrives for the Bambi 2013 media awards in Berlin, Germany, Thursday, Nov. 14, 2013. (AP Photo/Gero Breloer) 
    </summary> 
    <rights type="html"> 
     UK picture buyers only BRO105 
    </rights> 
    <id> 
     http://images.pressassociation.com/meta/2.18207923.xml 
    </id> 
    <title type="html"> 
     2013 Bambi Media Awards - Berlin 
    </title> 
    <category term="E"></category> 
    <author> 
     <name> 
      Gero Breloer/AP 
     </name> 
    </author> 
</entry> 

爲了得到媒體:命名空間節點,我使用這個查詢入口節點的其餘部分:「/命名空間:飼料/命名空間:入門|名稱空間:進入/介質:*」然而這樣做返回媒體節點作爲單獨的數組項:

rray 
(
    [0] => SimpleXMLElement Object 
     (
      [link] => Array 
       (
        [0] => SimpleXMLElement Object 
         (
          [@attributes] => Array 
           (
            [href] => http://images.pressassociation.com/meta/2.18209006.xml 
            [type] => application/vnd.iptc.g2.newsitem+xml 
           ) 

         ) 

        [1] => SimpleXMLElement Object 
         (


[@attributes] => Array 
          (
           [href] => http://images.pressassociation.com/meta/2.18209006.html 
           [rel] => related 
           [type] => text/html 
          ) 

        ) 

       [2] => SimpleXMLElement Object 
        (
         [@attributes] => Array 
          (
           [href] => http://images.pressassociation.com/empicsthumbnail/vol183/block3642/18209006.jpg 
           [rel] => related 
           [type] => image/jpeg 
          ) 

        ) 

      ) 

     [updated] => 2013-11-15T10:56:44Z 
     [summary] => Fans wait for singer Miley Cyrus before the Bambi 2013 media awards in Berlin, Germany, Thursday, Nov. 14, 2013. (AP Photo/Gero Breloer) 
     [rights] => UK picture buyers only BRO110 
     [id] => http://images.pressassociation.com/meta/2.18209006.xml 
     [title] => 2013 Bambi Media Awards - Berlin 
     [category] => SimpleXMLElement Object 
      (
       [@attributes] => Array 
        (
         [term] => E 
        ) 

      ) 

     [author] => SimpleXMLElement Object 
      (
       [name] => Gero Breloer/AP 
      ) 

    ) 

[1] => SimpleXMLElement Object 
    (
     [@attributes] => Array 
      (
       [url] => http://images.pressassociation.com/empicsthumbnail/vol183/block3642/18209006.jpg 
       [medium] => image 
       [type] => image/jpeg 
       [width] => 127 
       [height] => 190 
      ) 

    ) 

[2] => SimpleXMLElement Object 
    (
     [@attributes] => Array 
      (
       [url] => http://images.pressassociation.com/image/preview/2.18209006.jpg 
       [medium] => image 
       [type] => image/jpeg 
       [expression] => sample 
       [width] => 511 
       [height] => 767 
      ) 

    ) 

[3] => SimpleXMLElement Object 
    (
    ) 

[4] => SimpleXMLElement Object 
    (
     [@attributes] => Array 
      (
       [url] => http://images.pressassociation.com/image/2.18209006.jpg 
       [medium] => photo 
       [type] => image/jpeg 
       [expression] => full 
       [width] => 6041 
       [height] => 4024 
      ) 

    ) 

我然而需要[1] [2] [3] & [4]被嵌套withing條目節點[0]用於提取值。如果有人能夠幫助解決這個問題,我將非常感激。如果可能,我希望能夠通過一個XPath調用將其全部退回。

回答

1

要獲得進入節點只使用表達式的第一部分:

/atom:feed/atom:entry 

要獲得有媒體後代節點的所有入口節點,你可以使用這個表達式:

/atom:feed/atom:entry[.//media:*] 

後你得到入口節點,你想使用另一個表達式,並在query()/ evaluate()的第二個參數中提供上下文。

.//media:* 

請注意,您必須在SimpleXml/DOMXpath實例上註冊並使用自己的名稱空間前綴。

我建議使用DOMXpath ::評估(),而不是的SimpleXMLElement :: XPath的()。與xpath()不同,evaluate可以直接返回標量值。表達式string(.//media:copyright)僅適用於evaluate(),不適用於xpath()或DOMXpath :: query()。

下面是使用DOM + XPath來查詢與MediaRSS的Atom提要一個小例子:

$dom = new DOMDocument(); 
$dom->load('feed.xml'); 
$xpath = new DOMXpath($dom); 

$xpath->registerNamespace('atom', 'http://www.w3.org/2005/Atom'); 
$xpath->registerNamespace('media', 'http://search.yahoo.com/mrss/'); 

foreach ($xpath->evaluate('/atom:feed/atom:entry[.//media:thumbnail]', NULL, FALSE) as $entry) { 
    $href = $xpath->evaluate('string(atom:link[@rel = "related" and @type="text/html"]/@href)', $entry, FALSE); 
    $src = $xpath->evaluate('string(.//media:thumbnail/@url)', $entry, FALSE); 
    printf('<a href="%s"><img src="%s"/></a>', $href, $src); 
} 
+0

喜三江源的答覆,我很欣賞回答這個問題所花費的時間,但我是不明確你是什​​麼意思。這是如何在一個查詢中通過不同名稱空間查詢多個節點? – dickturnip

+0

我添加了一個小例子,顯示帶有鏈接的縮略圖。 – ThW

+0

非常好,昨晚我設法以一種不同的方式工作,但這也是一個很好的答案,我也會將它添加到我的工具包中,謝謝你的時間 – dickturnip

相關問題