2017-05-15 32 views
1

我使用xpath從epg文件中查找內容,但對於此源,我的代碼根本無法工作。現在我已經到了我無法自己解決這個問題的地步。 XML看起來像這樣(如您所見,2個名稱空間,v3和v31)。使用xpath和命名空間獲取節點等於...的所有同級節點

<?xml version="1.0" encoding="UTF-8"?> 
<v3:schedule timestamp="2017-05-12T16:11:06.595Z" xmlns:v3="http://common.tv.se/schedule/v3_1"> 
<v3:from>2017-05-12T22:00:00.000Z</v3:from> 
<v3:to>2017-05-13T22:00:00.000Z</v3:to> 
... 
<v3:contentList> 
<v31:content timestamp="2017-05-12T16:11:06.595Z" xmlns:v31="http://common.tv.se/content/v3_1"> 
    <v31:contentId>content.1375706-006</v31:contentId> 
    <v31:seriesId>series.40542</v31:seriesId> 
    <v31:seasonNumber>3</v31:seasonNumber> 
    <v31:episodeNumber>6</v31:episodeNumber> 
    <v31:numberOfEpisodes>8</v31:numberOfEpisodes> 
    <v31:productionYear>2017</v31:productionYear> 
    ... 
    <v3:eventList> 
    <v31:event timestamp="2017-05-12T16:11:06.595Z" xmlns:v31="http://common.tv.se/event/v3_1"> 
    <v31:eventId>event.26072881</v31:eventId> 
    <v31:channelId>channel.24</v31:channelId> 
    <v31:rerun>true</v31:rerun> 
    <v31:live>false</v31:live> 
    <v31:hidden>false</v31:hidden> 
    <v31:description/> 
    <v31:timeList> 
    <v31:time type="public"> 
     <v31:startTime>2017-05-12T22:55:00.000Z</v31:startTime> 
     <v31:endTime>2017-05-12T23:55:00.000Z</v31:endTime> 
     <v31:duration>01:00:00:00</v31:duration> 
    </v31:time> 
    </v31:timeList> 
    <v31:contentIdRef>content.1375706-006</v31:contentIdRef> 
    <v31:materialIdRef>material.1010161108005267221</v31:materialIdRef> 
    <v31:previousEventList/> 
    <v31:comingEventList/> 
    </v31:event> 
    ... 
    <v3:materialList> 
    <v31:material timestamp="2017-05-12T16:11:06.595Z" xmlns:v31="http://common.tv.se/material/v3_1"> 
    <v31:materialId>material.1010161108005267221</v31:materialId> 
    <v31:contentIdRef>content.1375706-006</v31:contentIdRef> 
    <v31:materialType>tx</v31:materialType> 
    <v31:videoFormat>576i</v31:videoFormat> 
    <v31:audioList> 
    <v31:format language="unknown">stereo</v31:format> 
    </v31:audioList> 
    <v31:aspectRatio>16:9</v31:aspectRatio> 
    <v31:materialReferenceList> 
    </v31:materialReferenceList> 
    </v31:material> 
... 

而「contentIdRef」是保持不同元素(事件和材料)在一起的東西。 我想根據contentIdRef找到所有的數據。

我已經使用這個(在PHP):

$parent = $this->xmldata->xpath('//v31:event/v31:contentIdRef[.="content.1375706-006"]/parent::*') 

我也有試過

$parent = $this->xmldata->xpath('//v31:event/v31:contentIdRef[.="content.1375706-006"]/parent::*/child::*'); 

但是,第一種選擇只(帶的print_r)返回V31:事件 「時間戳」

第二個選擇返回11「simpleXMLobjects」是空的(爲什麼它們是空的??),所以根據對象的數量,我認爲我有「擊中現場」,但我找不到它們爲什麼是emp ty ....

是的,我在我的代碼中註冊了命名空間(我希望它很簡單)。

TLDR; 我想1.獲取所有CONTENTIDS從第一塊(V3:contentList), 2.獲得所有EVENTDATA每個內容識別, 3.讓所有materialdata每個內容ID ...

我真心希望你能幫助:/

回答

0

您是否在Xpath表達式中註冊了名稱空間的前綴?始終爲您正在使用的名稱空間註冊自己的前綴。 PHP默認註冊當前上下文節點的名稱空間定義。但是,這可以在文檔中的任何元素節點上更改,並且並非所有前綴都可以在文檔元素上定義。

$schedule = new SimpleXMLElement($xml); 
$schedule->registerXpathNamespace('s', 'http://common.tv.se/schedule/v3_1'); 
$schedule->registerXpathNamespace('e', 'http://common.tv.se/event/v3_1'); 

$events = $schedule->xpath(
    '//e:event[e:contentIdRef = "content.1375706-006"]' 
); 

foreach ($events as $event) { 
    echo $event->asXml(), "\n\n"; 
} 

或者與DOM:

$document = new DOMDocument(); 
$document->loadXml($xml); 
$xpath = new DOMXpath($document); 
$xpath->registerNamespace('s', 'http://common.tv.se/schedule/v3_1'); 
$xpath->registerNamespace('e', 'http://common.tv.se/event/v3_1'); 

$events = $xpath->evaluate('//e:event'); 
foreach ($events as $event) { 
    echo $document->saveXml($event), "\n\n"; 
} 
+0

你好, 是的,我註冊了他們父母$前右,像這個 - $> xmldata-> registerXpathNamespace。 :/ – DreamHawk

相關問題