There is no big difference between templating XML and HTML.
<Response>
<Msg>
Your number is: <?php echo htmlspecialchars($_GET['From']); ?>
and you typed: <?php echo htmlspecialchars($_GET['Body']); ?>
</Msg>
</Response>
If your PHP installation is configured to allow “short tags” (ie., just <?
on its own rather than <?php
) as being PHP code, then the XML Declaration will trip it up. This is what caused your error.
Whilst you can fix that problem by disabling short tags, it's probably best to remove the <?xml
declaration instead or as well. There is no reason to include an XML Declaration in an XML file anyway (unless you're using XML 1.1, or a character encoding that isn't UTF-8, which generally you'd want to avoid).
Just as in HTML, you need to escape <
and &
characters (and quotes, in attribute values) otherwise your markup gets broken. htmlspecialchars
works just as well for XML as it does for HTML. (You may want to define a function with a shorter name to do echo htmlspecialchars
, to cut down on typing.)