【问题标题】:Use Xpath to get content from linked file in current XML document使用 Xpath 从当前 XML 文档中的链接文件中获取内容
【发布时间】:2013-01-11 09:50:58
【问题描述】:

我有一个 XML 文档,它是所有博客条目摘录的提要,并且在每个条目中都有一个指向另一个 XML 文件的链接,该文件包含该特定条目的完整内容(更大的图像、全文等) .是否可以使用 XPATH 访问那些内部 XML 文档并从中获取值?

主文档如下所示:

    <Objects>
              <Item xml="doc.xml"></Item>  
                      // I would want to be able to access content 
                      // inside the document at Item/@xml
              <Item xml="doc2.xml"></Item>
    </Objects>

【问题讨论】:

  • 如果我使用的是 .NET(由于某些奇怪的原因,我被禁止使用 XSLT),我将在 XPath 评估上下文中设置变量并将两个 XML 文档(已经解析)。

标签: xml xpath feed


【解决方案1】:
<?php
$feed = "http://example.com/feed.xml";
if (file_exists($feed)) {

    $xml = simplexml_load_file($feed);
    $Objects = $xml->xpath('//Objects/Item[@xml]');
    foreach ($Objects as $O) {
$feed2 = "http://example.com/".$O."";
}

if (file_exists($feed2)) {
    $xml = simplexml_load_file($feed2);
$feed2path = $xml->xpath('//*/*');
echo $feed2path[@someid];
}

} 
?> 

像这样使用两个 xpath 和一个 for 每个?

【讨论】:

  • 我最终破解了我正在使用的模块并将其粘贴到其中。非常感谢!
  • 没问题,很高兴它有帮助。
【解决方案2】:

有一个document函数,但我不知道你能不能完全按照你描述的那样做

http://www.ibm.com/developerworks/library/x-tipcombxslt/

【讨论】:

  • document() 函数是一个标准的 XSLT 函数——它在纯 XPath 1.0 中不可用。 XPath 2.0 有一个doc() 函数,但似乎 OP 需要一个 XPath 1.0 解决方案。
  • 没错。我已经尝试过 doc() 但它不起作用,所以它必须是 1.0。
【解决方案3】:

在使用 document() 函数时,您可以使用属性中的值。

这个小例子很适合我:

Static.xml 与 xml 参考

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="cartoon2html.xsl"?>
<xml xml="cartoons.xml"/>

实际的 XML

<cartoons>
    <cartoon name="Donald Duck" publisher="Walt Disney" />
    <cartoon name="Mickey Mouse" publisher="Walt Disney" />
    <cartoon name="Batman" publisher="DC Comics" />
    <cartoon name="Superman" publisher="DC Comics" />
    <cartoon name="Iron Man" publisher="Marvel Comics" />
    <cartoon name="Spider-Man" publisher="Marvel Comics" />
</cartoons>

使用值的 XSL

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:variable name="document" select="/xml/@xml" />
    <xsl:variable name="cartoons" select="document($document)/cartoons" />

    <xsl:template match="/">
        <html>
            <head>
                <title>Cartoons</title>
                <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
            </head>
            <body>
                <xsl:apply-templates select="$cartoons" />
            </body>
        </html>
    </xsl:template>

    <xsl:template match="cartoons">
        <table>
            <xsl:apply-templates />
        </table>
    </xsl:template>

    <xsl:template match="cartoon">
        <tr>
            <td><xsl:value-of select="@name" /></td>
            <td><xsl:value-of select="@publisher" /></td>
        </tr>
    </xsl:template>

</xsl:stylesheet>

运行示例

您可以使用 xsltproc 运行它:# xsltproc cartoon2html.xsl static.xml

您也可以在 Firefox 浏览器中打开 static.xml 文件。

【讨论】:

  • 这是一个 XPath 问题 -- 不是 XSLT 问题。
猜你喜欢
  • 1970-01-01
  • 2012-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-22
  • 2012-04-03
  • 1970-01-01
相关资源
最近更新 更多