【问题标题】:Xpath Linked XML ValuesXpath 链接的 XML 值
【发布时间】:2011-10-11 04:03:12
【问题描述】:

我有一个主 XML 列表,它链接到我需要从中提取的其他 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 的 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>

是否可以在主提要上使用 XPath 从 href 中提取数据?我最初在看类似/news/newsListItem/@href/???/newsItem/headline 的东西,但我不确定那是正确的轨道。这甚至可以通过 XPath 实现吗?

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

【问题讨论】:

  • 不幸的是,我无法使用 php 解决这个问题。我似乎仅限于 1.0 功能。谢谢大家的回复。为每个人点赞(因为所有 3 个都是正确的)!

标签: php xml xpath


【解决方案1】:

很遗憾,这是不可能的。最好的办法是检索 href,加载第二个 xml 文件,然后检索您需要的信息。

【讨论】:

    【解决方案2】:

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

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

    它接受一个 URI 并返回一个文档节点。这应该有效;如果您需要更多详细信息,请告诉我。

    【讨论】:

    • 目前 PHP 只支持 XPath 1.0 函数
    【解决方案3】:

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

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

    【讨论】:

    • 目前 PHP 只支持 XPath 1.0 函数
    • 谢谢你 AurelioDeRosa。这也是我发现的!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-23
    • 1970-01-01
    • 1970-01-01
    • 2015-05-23
    • 2016-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多