tags:

views:

59

answers:

5
+1  Q: 

Proper XML Syntax

Is this a correct way to store an address in an XML file? If it is, how would I echo it in three lines like an address should be written using PHP. If not what is the correct way? (The file is not complete. I am only interested in the address.)

<store name="Pizza R Us" Store_address="16  Main St Anytown, US 00000">
<items>
 <pizzas>
  <topping>
   <name>Tomato and Cheese</name>
   <price>
    <small>$6.85</small>
    <large>$10.85</large>
   </price>
  </topping>
 </pizzas>
</items>
</store>
+2  A: 

It isn't quite well-formed XML: you have a space in the "Store address" attribute name.

XML data is usually highly structured, so for example, it seems a little odd to have the dollar signs in the prices. Also, the address is a single string instead of being split into components. Of course, it's up to you how you want to structure your data, and it depends a lot on what your application is.

How you would display the data depends entirely on what programming environment you are using, which you haven't told us.

Ned Batchelder
A: 

Does price refer to the toppings or to the entire pizza? If it refers to the entire pizza, why is it embedded within topping?

Robert Harvey
A: 

No, it is not well formed XML - attribute names cannot have spaces in them, so:

Store address="16  Main St Anytown, US 00000"

should be:

StoreAddress="16  Main St Anytown, US 00000"
anon
+1  A: 

In response to storing the address: The recommended way is to not store it as attributes, but you can. It is better to store relevant data in tags rather then as attributes for several reasons. The way you are doing it makes it more difficult to pull out just the street or city for a programmer. It is also harder to read, maintain, and is not as easy to change without breaking existing code.

For more detailed information read the following page. Especially the sections "My Favorite Way" and "Avoid XML Attributes?"

http://www.w3schools.com/xml/xml_attributes.asp

Derek Litz
A: 

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&#10;Anytown, US&#10;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", "&#10;", 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.

bobince