XML Elements
-- Learn not only technology, but also dreams!- Home
- HTML
- JavaScript
- CSS
- Vue
- React
- Python3
- Java
- C
- C++
- C#
- AI
- Go
- SQL
- Linux
- VS Code
- Bootstrap
- Git
- Local Bookmarks
XML Tutorial
XML Tutorial XML Introduction XML Usage XML Tree Structure XML Syntax XML Elements XML Attributes XML Validation XML Validator View XML File XML CSS XML XSLTXML JavaScript
XML HTTP Request XML Parser XML DOM XML/HTML XML ApplicationsAdvanced XML
XML Namespaces XML CDATA XML Encoding XML Server XML DOM Advanced XML Notes XML Technologies XML Real Life XML Editors XML E4X XML Summary XML Examples XML Syntax XML AttributesDeep Exploration
Programming
Development Tools
Computers
Scripting Languages
Programming Languages
Software
Computer Hardware
Web Design & Development
Computer Science
Web Services
XML Elements
XML documents contain XML elements.
What is an XML Element?
An XML element refers to everything from (and including) the start tag to (and including) the end tag.
An element can contain:
- Other elements
- Text
- Attributes
- Mixtures of all 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, both <bookstore> and <book> have element content, because they contain other elements. The <book> element also has attributes (category="CHILDREN"). <title>, <author>, <year>, and <price> have text content, because they contain text.
XML Naming Rules
XML elements must follow these naming rules:
- Names can contain letters, numbers, and other characters
- Names cannot start with a digit or punctuation character
- Names cannot start with the letters xml (or XML, Xml, etc.)
- Names cannot contain spaces
You can use any name, there are no reserved words.
Best Naming Habits
Make the names descriptive. Using underscores in the name is also fine: <first_name>, <last_name>.
Names should be short and simple: <book_title> instead of <the_title_of_the_book>.
Avoid "-" characters. If you name something like "first-name", some software will think you want to subtract name from first.
Avoid "." characters. If you name something like "first.name", some software will think "name" is a property of "first".
Avoid ":" characters. Colons are reserved for use with namespaces (explained later).
XML documents often have a corresponding database where fields correspond to XML elements. A useful practice is to use the naming conventions from your database for the elements in your XML document.
In XML, non-English letters like éòÑ are completely legal, though you need to be aware that problems may arise if your software supplier does not support these characters.
XML Elements are Extensible
XML elements are extensible, to carry more information.
Take a look at the following XML example:
<note>
<to>Tove</to>
<from>Jani</from>
<body>Don't forget me this weekend!</body>
</note>
Let's imagine we create an application that 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 the author of the XML document adds some extra information:
<note>
<date>
```
YouTip