如果该节点的内容与另一个节点(兄弟节点)匹配,我试图显示节点的文本内容。这是XML:
<MemberId>
<BillingInfo>
<Details>
<Line NUMBER="0">
<ChargeDate>20140605</ChargeDate>
<DueDate>20140605</DueDate>
<TrackingNumber>PR DED</TrackingNumber>
<Description>Account payroll deduction</Description>
<RevCode>SJ1550</RevCode>
<RevDesc>ACCOUNT PAYROLL DEDUCTION</RevDesc>
<SalesAmount>-11.77</SalesAmount>
<ServiceCharge>0.00</ServiceCharge>
<GST>0.00</GST>
<PST>0.00</PST>
<Othertaxes>0.00</Othertaxes>
<Total>-11.77</Total>
<ChitCode>PR DED</ChitCode>
<ChitDate>20140605</ChitDate>
</Line>
<Line NUMBER="1">
<ChargeDate>20140605</ChargeDate>
<DueDate>20140605</DueDate>
<TrackingNumber>03128862</TrackingNumber>
<Description>Mountain Mkt Sales</Description>
<RevCode>SJ1553</RevCode>
<RevDesc>MOUNTAIN MKT SALES</RevDesc>
<SalesAmount>8.99</SalesAmount>
<ServiceCharge>0.00</ServiceCharge>
<GST>0.00</GST>
<PST>0.61</PST>
<Othertaxes>0.00</Othertaxes>
<Total>9.60</Total>
<ChitCode>03128862</ChitCode>
<ChitDate>20140605</ChitDate>
</Line>
</Details>
</BillingInfo>
<Chits>
<Chit>
<Code>03128862</Code>
<Body><![CDATA[
Member#: 0ECHER
Name: *******
Area: *********
Server: JEANNEANE
Chit#: 03128862
Date: June 5 2014
------- ------- ------- ------- -------
1 MINGUA BEEF JER 8.99
------- ------- ------- ------- -------
Sub-Total 8.99
TAX 0.61
------- ------- ------- ------- -------
Chit Total 9.60
MEMBER CHARGE -9.60
]]></Body>
</Chit>
</Chits>
</MemberId>
我成功显示了每个ChitCode和ChitDate,因为它们位于Line [@number]节点中。我想显示包含文本&#34; Chit#:XXXXXX&#34;的Body节点。其中xxxxxx与Line-&gt; ChitCode匹配。这是我的php,它显示Body,但它显示所有行项目的第一个Body:
<?php
foreach($Chitquery as $Chit)
{
foreach($Linequery as $Line)
{
echo "<label class='collapse' for='".$Line->ChitCode."-".$Line->ChitDate."'>".$Line->Description."</label>
<input id='".$Line->ChitCode."-".$Line->ChitDate."' type='checkbox'>
<div>";
// Creates hide - show link for each Description item
// Start getting the matching Chit Code from Chit/Body
if ($Chit->Code = $Line->ChitCode)
{
echo $Chit->Body."</div>";
}
else
{
echo " ".$Line->ChitCode."</div>";
} // End If
} //End foreach Linequery
} // End foreach Chitquery
?>
它是echo $ Chit-&gt; Body。&#34;&#34 ;;我需要帮助,我想显示包含$ Line-&gt; ChitCode值的Body。
哦,是的,由于“foreach”的不当放置,这些线条重复得很奇怪。声明! 救命!提前谢谢!
更新:我已经改变了我的PHP代码,现在我有隐藏显示div工作,没有重复,但我得到了#array;#39;而不是$ ChitBody。我无法弄清楚如何在$ ChitBody = $ member-&gt; xpath中找到包含$ Line-&gt; ChitCode的Body节点(&#39; Chits / Chit / Body [text()=&#34;&# 39;。$ Line-&gt; ChitCode。&#39;&#34;]&#39;);
<?php
foreach($Linequery as $Line) {
$ChitCode = $member->Chits->Chit->Code;
$ChitBody = $member->xpath('Chits/Chit/Body[text()="'.$Line->ChitCode.'"] ');
echo "<label class='collapse' for='".$Line->ChitCode."-".$Line->ChitDate."'>".$Line->Description."</label>
<input id='".$Line->ChitCode."-".$Line->ChitDate."' type='checkbox'>"; // Creates hide - show link for each Description item
// Start getting the matching Chit Code from Chit/Body
if ($ChitCode = $Line->ChitCode) {
echo "<div>Code: ".$ChitCode."<br>".$ChitBody."</div>";
} else {
echo "<div>".$Line->ChitCode."</div>";
} // End If
} //End foreachLinequery
?>
答案 0 :(得分:1)
您很可能正在寻找xpath来查询您的文档。
例如迭代线 - 我不完全理解你为什么这样做,但是这是我理解它的方式(仅文本输出):
$xml = simplexml_load_string($buffer);
foreach ($xml->xpath('//Details/Line') as $line)
{
printf("#%d: %s\n", $line['NUMBER'], $line->Description);
}
给出:
#0: Account payroll deduction
#1: Mountain Mkt Sales
我猜你看到这一般是如何运作的。所以现在根据$line->ChitCode
获取chit,我认为实际上对你来说更有趣。它还更好地说明了xpath的好处:
foreach ($xml->xpath('//Details/Line') as $line)
{
printf("#%d: [%s] %s\n", $line['NUMBER'], $line->ChitCode, $line->Description);
$query = sprintf("//Chits/Chit[Code = '%s']/Body", $line->ChitCode);
$result = $xml->xpath($query);
if ($result) {
$chit = $result[0];
echo $chit;
} else {
echo ' -- no chit found -- ';
}
echo "\n";
}
这次查询包含查找数字的条件(如果要进行此保存,escape the string properly(more info)),它基本上会创建以下两个xpath查询:
//Chits/Chit[Code = 'PR DED']/Body
//Chits/Chit[Code = '03128862']/Body
我再次猜你可以想象那些意味着什么。如果找到这样的<Body>
元素,则上面的代码输出它。这就是结果的样子:
#0: [PR DED] Account payroll deduction
-- no chit found --
#1: [03128862] Mountain Mkt Sales
Member#: 0ECHER
Name: *******
Area: *********
Server: JEANNEANE
Chit#: 03128862
Date: June 5 2014
------- ------- ------- ------- -------
1 MINGUA BEEF JER 8.99
------- ------- ------- ------- -------
Sub-Total 8.99
TAX 0.61
------- ------- ------- ------- -------
Chit Total 9.60
MEMBER CHARGE -9.60
我希望这能让你有足够的洞察力来开始它。 xpath()
is just a method on SimpleXMLElement因此你可以将这个与我没有复制的代码结合起来,只是为了让示例更小(我在这里使用纯文本输出的原因相同,将与HTMl同样工作,但会使示例膨胀)。
OP增加:
工作量很棒!!
我将您的代码改编为此代码,并且完美运行:
foreach ($Linequery as $Line) {
$LineChitCode = $Line->ChitCode;
$ChitBody = sprintf("//Chits/Chit[Code = '%s']/Body", $LineChitCode);
$CBresult = $member->xpath($ChitBody);
echo "<label class='collapse' for='" . $Line->ChitCode . "-" . $Line->ChitDate . "'>" . $Line->Description . "</label>
<input id='" . $Line->ChitCode . "-" . $Line->ChitDate . "' type='checkbox'>"; // Creates hide - show link for each Description item
// Start getting the matching Chit Code from Chit/Body
if ($CBresult) {
$chit = $CBresult[0];
echo "<div>Code: " . $LineChitCode . "<br>" . $chit . "</div>";
} else {
echo "<div>" . $Line->ChitCode . "</div>";
}
}
谢谢你,你在3天的时间里救了我,想要解决这个问题!!
答案 1 :(得分:0)
你可以使用if($ Chit-&gt; Code == $ Line-&gt; ChitCode)而不是if($ Chit-&gt; Code = $ Line-&gt; ChitCode)