In XML (eXtensible Markup Language), tags and attributes are fundamental components used to structure and describe data within XML documents.
-
Tags:
- Tags are enclosed within angle brackets
< >
and are used to define the beginning and end of elements in XML. - Tags can be nested within each other to create a hierarchical structure, forming an XML tree.
- Every XML document has one root element, which encapsulates all other elements within it.
Example:
<bookstore> <book> <title>Harry Potter and the Philosopher's Stone</title> <author>J.K. Rowling</author> </book> <book> <title>The Hobbit</title> <author>J.R.R. Tolkien</author> </book> </bookstore>
- Tags are enclosed within angle brackets
-
Attributes:
- Attributes provide additional information about an element and are written within the start tag of an element.
- Attributes consist of a name-value pair separated by an equals sign
=
and enclosed within double or single quotes. - Elements can have zero or more attributes.
Example:
<book isbn="9780439554930"> <title>Harry Potter and the Philosopher's Stone</title> <author>J.K. Rowling</author> </book>
In this example, isbn
is an attribute of the <book>
element. The value "9780439554930"
provides additional information about the book.