XML technical m...login
XML technical manual
author:php.cn  update time:2022-04-14 15:57:53

XML elements



XML documents contain XML elements.


What is an XML element?

XML element refers to the part from (and including) the start tag to (and including) the end tag.

An element can contain:

  • Other elements

  • Text

  • Properties

  • or mix all of the above...

<bookstore>
<book category="CHILDREN">
​ <title>Harry Potter</title>
​​ <author>J K. Rowling</author>
​ <year>2005</year>
​ <price>29.99</price>
</book>
<book category="WEB">
​ <title>Learning XML</title>
​ <author>Erik T. Ray</author>
​ <year>2003</year>
​ <price>39.95</price>
</book>
</bookstore>

In the example above, <bookstore> and <book> both have element content because they contain other elements. The <book> element also has the attribute (category="CHILDREN"). <title>, <author>, <year> and <price> have text content because they contain text.


XML naming rules

XML elements must follow the following naming rules:

  • Names can contain letters, numbers, and other characters

  • The name cannot start with a number or punctuation mark

  • The name cannot start with the letters xml (or XML, Xml, etc.)

  • Name cannot contain spaces

Any name can be used, no reserved words.


Best Naming Practices

Make the name descriptive. It's also good to use underscores in names: <first_name>, <last_name>.

Names should be short and simple, such as: <book_title>, not: <the_title_of_the_book>.

Avoid the "-" character. If you name something like this: "first-name", some software will think you want to subtract name from first.

Avoid the "." character. If you name it like this: "first.name", some software will assume that "name" is a property of the object "first".

Avoid the ":" character. The colon will be converted to a namespace for use (described later).

XML documents often have a corresponding database, whose fields correspond to elements in the XML document. A practical rule of thumb is to use the naming conventions of your database to name elements in your XML document.

In XML, non-English letters such as éòá are completely legal, but you need to be aware of problems that may arise if your software vendor does not support these characters.


XML elements are extensible

XML elements are extensible to carry more information.

Please see the following XML example:

<note>
<to>Tove</to>
<from>Jani</from>
<body>Don't forget me this weekend!</body>
</note>

Let's imagine that we create an application that can Extracts the <to>, <from> and <body> elements from the XML document and produces the following output:

MESSAGE
To: Tove
From: Jani
Don't forget me this weekend!

Imagine that the author of the XML document added some extra information:

<note>
<date>2008-01-10</date>
< to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body> ;
</note>

So will this app break or crash?

Won't. This application can still find the <to>, <from>, and <body> elements in the XML document and produce the same output.

One of the advantages of XML is that it can be extended without interrupting the application.

php.cn