基于父节点存储xml子节点的内容';s属性


Storing the content of an xml child node based on a parent node's attribute in php

我正在尝试显示从xml结果返回的最大图像url。到目前为止,返回的最大值是400高,所以我硬编码了400。如果可能的话,我想只选择最大值,以防将来我得到的结果中没有400高的图像。

我试过

$x = file_get_contents($url);
$xml = simplexml_load_string($x);
$imageURL=$xml->categories->category->items->product->images->image[@height='400']->sourceURL;

这给了我"语法错误,意外的'=',应为']'"。

我也试过:

$imageURL= $xml->xpath("/categories/category/items/producct/images/image[@height='400']/sourceURL");

但链接不好。这是XML:

 <images>
        <image available="true" height="100" width="100">
            <sourceURL>
                Someurl.com
            </sourceURL>
        </image>
        <image available="true" height="200" width="200">
            <sourceURL>
                Someurl.com
            </sourceURL>
        </image>
        <image available="true" height="300" width="300">
            <sourceURL>
                Someurl.com
            </sourceURL>
        </image>
        <image available="true" height="400" width="400">
            <sourceURL>
                Someurl.com
            </sourceURL>
        </image>
        <image available="true" height="399" width="400">
            <sourceURL>
                Someurl.com
            </sourceURL>
        </image>
    </images>

有什么想法吗?

->image[@height='400']是一个直接的PHP数组引用。这将被解释为在defined()常数(height)上抑制错误(@),并试图通过赋值='400'设置其值。

对于您的xpath版本,请记住,xpath查询返回的是DOMNodeList,而不是实际的DOMElement。要从查询结果中获得所需的URL,您必须遍历节点列表:

$nodes = $xpath->query(...) {
foreach($nodes as $node) {
   $url = $node->nodeValue;
}
Below code might help...

    $xmlSQLProcedures = new DOMXPath($xmlSQLProcedures);
    $strProcedureName = $xmlSQLProcedures->query("//SQLProcedure[@ID='$sSQLProcedureID']")->item(0)->nodeValue;
    $nodeParameters = $xmlSQLProcedures->query("//SQLProcedure[@ID='$sSQLProcedureID']/Parameters/Parameter");
    $ParamCount = $nodeParameters->length-1;
    for ($i=0;$i<=$ParamCount;$i++) {
        echo $nodeParameters->item($i)->getAttribute("Name").'<br>';
    }

<?xml version="1.0" encoding="UTF-8"?>
<SQLProcedures>
<!--    **********   FOR KEYWORD IN LOCAL LANGUAGE    *************    -->
    <SQLProcedure ID="001070001">
        <Name>P_ManipulateKeywordsInLL</Name>
        <Parameters>
            <Parameter Name="LanguageId"/>
            <Parameter Name="KeywordId"/>
            <Parameter Name="KeywordInLL"/>
            <Parameter Name="ActionFor"/>
            <Parameter Name="KeywordInLLId"/>
            <Parameter Name="Keyword"/>
            <Parameter Name="KeywordList"/>
            <Parameter Name="SessionId"/>
            <Parameter Name="WarehouseId"/>
        </Parameters>
    </SQLProcedure>
</SQLProcedures>