YouTip LogoYouTip

Xml Syntax

```html XML Syntax

XML syntax rules are simple and logical.

These rules are easy to learn and use.

XML Declaration

The optional XML declaration should be placed at the first line of the document if it exists:

<?xml version="1.0" encoding="utf-8"?>

The above example includes the XML version (version="1.0") and even the character encoding (encoding="utf-8").

UTF-8 is also the default encoding for HTML5, CSS, JavaScript, PHP, and SQL.

XML Document Must Have a Root Element

An XML document must contain a root element, which is the parent element of all other elements, like in the following example where "root" is the root element:

<root><child><subchild>.....</subchild></child></root>

In the following example, "note" is the root element:

<?xml version="1.0" encoding="UTF-8"?><note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>

An XML document consists of elements, each including a start tag, an end tag, and element content.

Attributes

Elements can include attributes, which provide additional information about the element.

Attributes are located within the start tag, such as:

<person age="30" gender="male">John Doe</person>

XML Single Tags

All XML elements generally have a closing tag, but single tags are also allowed.

The notation for single tags is as follows:

<elementName attribute="value" />

A single tag refers to a tag that simultaneously contains both the start and end tags, resembling the empty element tags in HTML.

In XML, you can represent single tags using two forms:

  • Using an empty element tag:
  • <exampleTag />
  • Using start and end tags, but without any content:
  • <exampleTag></exampleTag>

Both forms are equivalent, and you can choose one based on personal or project conventions.

Many XML parsers interpret these two forms as the same structure.

Example

<lineBreak />

Here, <lineBreak /> is a single tag representing a newline operation.

Single tag with attributes:

<image src="logo.png" alt="Logo" />

This tag represents an image element, containing the src and alt attributes, indicating the path and alternative text of the image.

XML File Example

Example

<?xml version="1.0" encoding="UTF-8"?>

<root>

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

<emptyElement attribute="value"/>

</root>

In this example, <note> and <emptyElement /> are both single tags. The <note> tag serves to pass information, while <emptyElement /> is an empty tag without child elements.

XML Tags Are Case Sensitive

XML tags are case sensitive. The tag <Letter> is different from the tag <letter>.

Must write the opening and closing tags in the same case:

<Message>This is wrong</message><message>This is right</message>

Note: Opening and closing tags are usually referred to as start and end tags. Regardless of which term you prefer, their concepts are the same.

XML Elements Must Be Properly Nested

Unlike HTML, XML elements must be properly nested:

<b><i>This text is bold and italic</i></b>

In the above example, proper nesting means: Since the <i> element is opened inside the <b> element, it must be closed inside the <b> element.

XML Attribute Values Must Be Quoted

Similar to HTML, XML elements can have attributes (name/value pairs).

In XML, attribute values must be enclosed in quotes.

Study the following two XML documents. The first one is incorrect, and the second one is correct:

<note date=12/11/2007><to>Tove</to><from>Jani</from></note>
<note date="12/11/2007"><to>Tove</to><from>Jani</from></note>

The error in the first document is that the date attribute of the note element is not enclosed in quotes.

Entity References

In XML, some characters have special meanings.

If you place the character "<" in an XML element, an error will occur because the parser will treat it as the beginning of a new element.

This will produce an XML error:

<message>if salary < 1000 then</message>

To avoid this error, replace the "<" character with its **entity reference**:

<message>if salary < 1000 then</message>

In XML, there are five predefined entity references:

Entity Reference Description
< Less than
> Greater than
& And
' Apostrophe
" Quotation mark

Note: In XML, only the characters "<" and "&" are truly illegal. Greater than is legal, but replacing it with an entity reference is a good habit.

Comments in XML

The syntax for writing comments in XML is similar to HTML.

Spaces in XML Will Be Preserved

In HTML, multiple consecutive space characters are trimmed (merged) into one:

HTML: Hello Tove
Output result: Hello Tove

In XML, spaces in the document are not deleted:

<bookstore> <book> <title>xx xxx</title> <author>John Doe</author> <price>29.99</price> </book></bookstore>

The content read and displayed from the title tag is as follows:

xx xxx

XML Stores Newlines with LF

In Windows applications, newlines are typically stored as a pair of characters: carriage return (CR) and line feed (LF).

In Unix and Mac OSX, LF is used to store new lines.

In old Mac systems, CR is used to store new lines.

XML stores newlines with LF.

```
← Xml ElementsXml Tree β†’