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>
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;
}