I have a custom XML schema defined for page display that puts elements on the page by evaluating XML elements on the page. This is currently implemented using the preg regex functions, primarily the excellent preg_replace_callback function, eg:
...
$s = preg_replace_callback("!<field>(.*?)</field>!", replace_field, $s);
...
function replace_field($groups) {
return isset($fields[$group[1]) ? $fields[$groups[1]] : "";
}
Just as an example.
Now this works pretty well... so long as the XML elements aren't nested. At this point it gets a whole lot more complicated, like if you have:
<field name="outer">
<field name="inner">
...
</field>
</field>
You want to make sure you replace the innermost field first. Judicious use of greedy/non-greedy regex patterns can go some of the way to handling these more complicated scenarios but the clear message is that I'm reaching the limits of what regex can reasonably do and really I need to be doing XML parsing.
What I'd like is an XML transformation package that:
allows me to conditionally evaluate/include the contained document tree or not based on a callback function ideally (analagous to preg_replace_callback); can handle nested elements of the same or different types; and handles attributes in a nice way (eg as an associative array).
What can help me along the way?