As it stands there is no way to extract the line breaks from the attribute value in the given XML snippet. There are no line breaks in there, just spaces, with no way to tell a space that should indicate a newline from one that is just a space.
You can put newlines in attribute values. But to avoid the attribute value normalisation process squashing them down to spaces, they would have to have been escaped:
<store name="Pizza R Us" Store_address="16 Main St Anytown, US 00000">
Using a proper XML serialiser would do this for you automatically. If you're templating directly into XML, unfortunately there isn't an htmlspecialchars
flag to do this kind of escaping, so you have to say:
<store
name="<?php echo htmlspecialchars($name); ?>"
Store_address="<?php echo str_replace("\n", " ", htmlspecialchars($address)); ?>"
>
You might prefer to use element content instead of attribute values to store information like this, eg.:
<store>
<name><?php echo htmlspecialchars($name); ?></name>
<address><?php echo htmlspecialchars($address); ?></address>
<items>
...
</items>
</store>
As newlines in element content are preserved.