PHP使用XPath查找有价值的标签
问题描述:
How can I use XPath to get the value of <FL val="Account Owner"> by searching for the <FL val="Account Name"> tag?
I've been using the following PHP / XPath code:
$aan = "Company A";
$ContRow = "/results/Accounts/row['FL val=\"Account Name\"'='".$aan."']";
foreach ($Cxml->xpath($ContRow) as $Crow)
{
$ao = $Crow->FL[1];
echo $ao."<br />";
}
and the XML code:
<result>
<Accounts>
<row no="1">
<FL val="Account Name">
<![CDATA[Company A]]>
</FL>
<FL val="Account Owner">
<![CDATA[Owner's Name]]>
</FL>
</row>
</Accounts>
</result>
如何使用XPath获取&lt; FL val =“帐户所有者”&gt;的值 通过搜索&lt; FL val =“帐户名称”&gt; 我一直在使用以下PHP / XPath代码: p>
$ aan =“Company A”;
$ ContRow =“/ results / Accounts / row ['FL val = \”Account Name \“'='”。$ aan。“']”;
foreach($ Cxml-&gt; xpath($ ContRow)as $ Crow)
{
$ ao = $ Crow-&gt; FL [1];
echo $ ao。“&lt; br /&gt;”;
}
code> pre>
和XML代码: p>
&lt; result&gt;
&lt; Accounts&gt;
&lt; row no =“1”&gt;
&lt; FL val =“帐户名称”&gt;
&lt;![CDATA [公司A]]&gt;
&lt; / FL&gt;
&lt; FL val =“帐户所有者”&gt;
&lt;![CDATA [所有者的 名称]]&gt;
&lt; / FL&gt;
&lt; / row&gt;
&lt; / Accounts&gt;
&lt; / result&gt;
code> pre>
div>
答
The following expression will find every row
that satisfies having an <FL val="Account Name">
descendant that contains "Company A"
and then selects the <FL val="Account Owner">
element inside:
/result/Accounts/row[contains(FL[@val="Account Name"], "Company A")]/FL[@val="Account Owner"]
This expression should also do it:
/result/Accounts/row/FL[@val="Account Name" and contains(.,"Company A")]/following-sibling::FL
Example
$doc = new DOMDocument;
$doc->loadXml($xml);
$xpath = new DOMXPath($doc);
$res = $xpath->query('/result/Accounts/row[contains(FL[@val="Account Name"], "Company A")]/FL[@val="Account Owner"]');
foreach ($res as $node) {
echo $node->nodeValue;
}