Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

XML Elements

XML Elements


An XML document contains XML elements.


What Is an XML Element?

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

An element can contain:

<

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, and both have Element content, because they contain other elements. Elements also haveproperty(category="CHILDREN")。 、<author>、<year> and <price> Yes<strong>Text content</strong>, because they contain text.</p> <hr /> <h2>XML Naming Rules</h2> <p>XML elements must follow these naming rules:</p> <ul> <li>Names can contain letters, digits, and other characters</li> <li>A name cannot begin with a number or a punctuation mark</li> <li>Names cannot begin with the letters xml (or XML, Xml, etc.)</li> <li>Names cannot contain spaces</li> </ul> <p>Any name can be used, there are no reserved words.</p> <hr /> <h2>Best naming practices</h2> <p>Make names descriptive. Using names with underscores is also nice:<first_name>、<last_name>。</p> <p>The name should be short and simple, for example:<book_title>, instead of:<the_title_of_the_book>。</p> <p>Avoid the "-" character. If you name things this way: "first-name", some software will think you want to subtract name from first.</p> <p>Avoid the "." character. If you name things this way: "first.name", some software will consider "name" to be a property of the object "first".</p> <p>Avoid the ":" character. The colon is converted into a namespace for use (introduced later).</p> <p>XML documents often have a corresponding database, where the fields correspond to elements in the XML document. A useful rule of thumb is to use the database's naming conventions to name elements in the XML document.</p> <p>In XML, non-English letters such as éòá are perfectly legal, but you need to be aware of potential problems that may arise when your software vendor does not support these characters.</p> <hr /> <h2>XML elements are extensible</h2> <p>XML elements are extensible to carry more information.</p> <p>See the following XML example:</p> <pre><code>< note < to Tove </ to < from Jani </ from < body</code></pre><p>Don't forget me this weekend!</p> <pre><code></ body </ note</code></pre><p>Let's imagine we create an application that can extract <to> 、<from> and elements from an XML document and produce the following output:</from> </to></p> <p>| <strong>MESSAGE</strong></p> <p><b>To:</b> Tove <br> <b>From:</b> Jani</p> <p>Don't forget me this weekend!</p> | | --- | <p></p> <p>Imagine that the author of the XML document added some extra information:</p> <pre><code>< note < date 2008-01-10 date < to Tove to < from Jani from < heading Reminder heading < body</code></pre><p>Don't forget me this weekend!</p> <p><!--</p--> <p>body</p> <blockquote></blockquote> <p><!--</p--> <p>note</p> <blockquote></blockquote> <p>Will this application break or crash?</p> <p>No. The application can still find the <to> 、<from> and elements in the XML document and produce the same output.</from> </to></p> <p>One of the advantages of XML is that it can be extended without breaking applications.</p></article> </main> <footer>Source: <a href="https://www.runoob.com/xml/xml-elements.html" rel="nofollow noopener">Runoob Tutorial</a> · Original modified: 2017-03-14T11:40:47 · This copy is for personal offline learning only; public distribution is prohibited.</footer> </body> </html>