I would like to replace <includes module="styles" />
with the $styles string at the position where it is. Unfortunately it appends it in the body and not in the head. Anyway, maybe there is another way to realize this issue?
$xml = <<<EOD
<!DOCTYPE html>
<html>
<head>
<title></title>
<includes module="styles" />
</head>
<body>
<includes module="m1" />
<includes module="m2" />
</body>
</html>
EOD;
$styles = <<<EOD
<styles>
.m1{
font-size: 12px;
font-family: Helvetica, Arial, sans-serif;
color: blue;
}
</styles>
EOD;
$dom = new DOMDocument();
$dom->loadXML($xml);
$elements = $dom->getElementsByTagName('includes');
for ($i = $elements->length-1; $i >= 0; $i--) {
$element = $elements->item($i);
$newNode = $dom->createDocumentFragment();
$mod = $element->getAttribute('module');
if($mod==='styles'): $newNode->appendXML($styles); endif;
$element->parentNode->replaceChild($newNode, $element);
}
print $dom->saveXml($dom->documentElement);
cheers