嗨,我想将成本值更改为$ 6.0,其中蛋糕的颜色为红色。我怎么能做到这一点..在这里是我的两个样品蛋糕,虽然我有很多蛋糕在我的XML文件..所以我想先找到蛋糕的颜色是红色,然后更改相应的蛋糕价格,无论我喜欢什么..如何使用Dom更改xml中的特定节点值php
<cupcake>
<name> Cookies and Cream</name>
<flavours>
<ingredient>Chocolate Cake</ingredient>
<ingredient>Salted Caramel Buttercream</ingredient>
<ingredient>Buttercream</ingredient>
</flavours>
<colors>
<color>Red</color>
</colors>
<energy>1900.6Cal</energy>
<cost>$22.50</cost>
</cupcake>
<cupcake>
<name> Killer Carrot</name>
<flavours>
<ingredient>Carrot Spice cake</ingredient>
<ingredient>Cream Cheese Frosting</ingredient>
<ingredient>Candied Carrots</ingredient>
<ingredient>Chocolate</ingredient>
</flavours>
<colors>
<color>Aqua</color>
</colors>
<energy>1500.0KJ</energy>
<cost>$15.80</cost>
</cupcake>
和我的PHP文件是
<?php
$xml = new DOMDocument();
$xml->load('cupcakes.xml');
if ($xml->schemaValidate('cupcakes.xsd')==FALSE)
die ('<div class="error">Validation failed</div>');
$xsl = new DOMDocument();
$xsl->load('cupcakes.xsl');
$proc = new XSLTProcessor();
$proc->importStyleSheet($xsl); // attach the xsl rules
echo $proc->transformToXML($xml);
echo "<hr/>";
echo "<h2> the first cupcake having color red has changed the cost value to $6.0";
$a = $xml->getElementsByTagName('color');
foreach ($a->nodeValue as $A){
if ($A = "Red")
$a->getElementsByTagName('cost')->nodeValue="$6.00";
}
echo $proc->transformToXML($xml);
?>
使用xpath'// cupcake [.// color [text()=“Red”]]/cost'来查找需要的元素并更改值。也许你应该用'''包装你的xml。$ xml。' ' –
splash58