Home  >  Article  >  Backend Development  >  XML development basics-XML elements

XML development basics-XML elements

黄舟
黄舟Original
2017-03-25 17:04:251530browse

XMLThe document contains XML elements.

What is an XML element?

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

element can contain other elements, text, or a mixture of both. Elements can also have attributes.

<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>LearningXML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>

In the above example, 70520fc1addb88c1722487f5b44e82c9 and 463aef0d2da08708f472268a99530dbe both have element content because they contain other elements. 48fe722b397613e801e59f453d6c9330 has text content only because it contains only text.

In the above example, only the 463aef0d2da08708f472268a99530dbe element has the attribute (category="CHILDREN").

XML naming rules

XML elements must follow the following naming rules:

The name can contain letters, numbers and other characters. The name cannot start with numbers or punctuation marks. The name cannot start with The name starting with the characters "xml" (or XML, Xml) cannot contain spaces

Any name can be used, no reserved words.

Best Naming Practices

Make the name descriptive. It's also a good idea to use underscores in names.

The name should be short, such as: 81fed6f73f5f0ce942cf29672fbb1288, not: fcbac7bddcc47eda8b997fba538ca23d.

Avoid the "-" character. If you name it like this: "first-name", some software will think you need to extract the first word.

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

Avoid the ":" character. The colon will be converted to 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 database's naming rules to name elements in an XML document.

Non-English letters such as éòá are also legal XML element names, but you need to be aware of problems that may arise when software developers do not support these characters.

XML elements are extensible

XML elements are extensible to carry more information.

Look at the following XML example:

<note>
<to>George</to>
<from>John</from>
<body>Don&#39;t forget the meeting this weekend!</body>
</note>

Let's imagine that we create an application that extracts the 05034471df6398a62d5708f78b78e0ac, 66fd2ada9ebb04d4250c850dc1e3737e and 6c04bd5ca3fcae76e30b72ad730ca86d elements out, and produces the following output:

MESSAGE
To: George
From: John
Don&#39;t forget the meeting this weekend!

Imagine that the author of this XML document later adds some additional information to this document:

<note>
<date>2008-08-08</date>
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don&#39;t forget the meeting this weekend!</body>
</note>

Will the application break or crash? ?

Won't. This application can still find the 05034471df6398a62d5708f78b78e0ac, 66fd2ada9ebb04d4250c850dc1e3737e, and 6c04bd5ca3fcae76e30b72ad730ca86d elements in the XML document and produce the same output.

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

The above is the detailed content of XML development basics-XML elements. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn