Xpath链接的XML值

时间:2011-10-10 19:13:39

标签: php xml xpath

我有一个主XML列表,链接到我需要从中提取的其他XML实体。以下是两个Feed的示例:

    <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的url指向辅助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>

是否可以在主Feed上使用XPath从hrefs中提取数据?我原本看起来像/news/newsListItem/@href/???/newsItem/headline,但我不确定这是正确的轨道。这甚至可以用XPath吗?

任何建议都将不胜感激。这是我第一次使用XPath。

3 个答案:

答案 0 :(得分:1)

不幸的是,这是不可能的。您可以做的最好的事情是检索href,加载第二个xml文件,然后检索所需的信息。

答案 1 :(得分:1)

有一个带有此签名的XPath函数doc():

fn:doc($ uri as xs:string?)as document-node()?

它需要一个URI并返回一个文档节点。这应该工作;如果您需要更多详细信息,请与我们联系。

答案 2 :(得分:1)

可能你问的是 fn:doc($ uri as xs:string?)作为document-node()?函数(在xPath 2.0中可用)。 不确定是否有效 - 但您的请求可以改写为:

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