Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Xpath从当前XML文档中的链接文件获取内容_Xml_Xpath_Feed - Fatal编程技术网

使用Xpath从当前XML文档中的链接文件获取内容

使用Xpath从当前XML文档中的链接文件获取内容,xml,xpath,feed,Xml,Xpath,Feed,我有一个XML文档,它是所有博客条目摘录的提要,在每个条目中都有一个指向另一个XML文件的链接,该文件保存该特定条目的完整内容(较大的图像、全文等)。是否可以使用XPATH访问这些内部XML文档并从中获取值 主文档如下所示: <Objects> <Item xml="doc.xml"></Item> // I would want to be able to access co

我有一个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>

//我希望能够访问内容
//在Item/@xml处的文档中

有一个
文档
功能,但我不知道您是否能准确地完成所描述的内容

xpath('//Objects/Item[@xml]');
foreach($O形式的对象){
$feed2=”http://example.com/“$O.”;
}
如果(文件_存在($feed2)){
$xml=simplexml\u加载文件($feed2);
$feed2path=$xml->xpath('/*/*/*');
echo$feed2path[@someid];
}
} 
?> 

使用两个XPath和一个for-each可以实现类似的功能?

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

这个小例子很适合我:

带xml引用的Static.xml

实际的XML

使用该值的XSL

卡通片
运行样本 您可以使用xsltproc:#xsltproc carton2html.xsl static.xml运行此操作


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

这是一个XPath问题,而不是XSLT问题。
document()
函数是一个标准XSLT函数,在纯XPath 1.0中不可用。XPath 2.0有一个
doc()
函数,但是OP似乎需要一个XPath 1.0解决方案。我尝试过doc(),但它不起作用,所以它必须是1.0。如果我使用.NET(由于某种奇怪的原因,我被禁止使用XSLT),我会在XPath计算上下文中设置变量,并将两个XML文档(已经解析)指定为它们的值。我最终破解了我正在使用的模块,并在中粘贴了一个版本。谢谢!没问题,很高兴它有帮助。
<?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];
}

} 
?> 
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="cartoon2html.xsl"?>
<xml xml="cartoons.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>
<?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>