2011-10-10 57 views
0

我有主XML列表,鏈接到我需要從其他XML實體鏈接。下面是兩種飼料的例子:Xpath鏈接的XML值

<news state="Trial"> 
     <newsListItem href="http://api.newsfeed.com/x/news/800614671/"> 
     <id>800614671</id> 
     <publishDate>2011-10-10T14:17:08</publishDate> 
     </newsListItem> 
     <newsListItem....> // Many more news items 
    </news> 

的newsListItem點到次級xml文件,看起來像的網址:

<newsItem encoding="UTF-8"> 
     <id>800614671</id> 
     <state>Trial</state> 
     <publishDate>2011-10-10T14:17:08</publishDate> 
     <createdDate>2011-10-10T13:18:02</createdDate> 
     <lastModifiedDate>2011-10-10T14:18:38</lastModifiedDate> 
     <headline>Woman stabbed to death in south London</headline> 
     <extract>Officers have confirmed a woman has been stabbed to death in a Bexleyheath street this morning.</extract> 
     <categories href="http://api.newsfeed.com/x/news/800614671/categories/"/> 
     <text format="html"><p>By Guy Kilty</p> 
     <p>A woman has been stabbed to death and another has been injured in an attack in south London, police have confirmed.</p> 
     <p>Officers were called to a busy street near the Asda supermarket in the Broadway area of Bexleyheath at 08:30 BST this morning (October 10th) following reports that a woman had been stabbed.</p> 
     <p>The first victim was found with a stab wound and died at the scene, while the second had been stabbed in the hand and was taken to hospital.</p> 
     <p>A Metropolitan Police spokesman said: &quot;Officers and the London Ambulance Service arrived on the scene and found a woman suffering stab wounds, who was pronounced dead at 8.41am.</p> 
     <p>&quot;A second female suffered a knife injury to her hand. One woman has been arrested in connection with the incident.&quot;</p> 
     <p>Police had no immediate information about the age of any of the people involved or any reason for the attack.</p> 
     </text> 
    </newsItem> 

是否可以使用XPath的主要飼料拉來自hrefs的數據?我最初看起來像/news/newsListItem/@href/???/newsItem/headline,但我不確定這是正確的軌道。這甚至可以用XPath嗎?

任何意見將不勝感激。這是我第一次使用XPath。

+0

不幸的是,我不能用PHP來拉這個。我似乎只限於1.0功能。謝謝大家的回覆。 Upvotes for everyone(因爲所有3個都是對的)! –

回答

1

不幸的是,這是不可能的。你可以做的最好的是檢索href,加載第二個xml文件,然後檢索你需要的信息。

1

有一個XPath函數(DOC)與此簽名:

FN:DOC($ URI爲XS:字符串)作爲文檔節點()?

它接受一個URI並返回一個文檔節點。這應該工作;讓我知道你是否需要更多細節。

+0

目前PHP只支持XPath 1.0函數 –

1

可能你問的是fn:doc($ uri as xs:string?)as document-node()?函數(可在xPath 2.0中使用)。 不知道它會工作 - 但你的要求可以改寫爲:

fn:doc(/news/newsListItem/@href)/newsItem/headline 
+0

目前PHP只支持XPath 1.0函數 –

+0

謝謝AurelioDeRosa。這也是我發現的! –