XML attributes with an example:
Consider an XML document representing information about a product:
<product id="12345" category="Electronics" brand="Samsung"> <name>Smartphone</name> <price currency="USD">500</price> </product>
In this XML document, the <product>
element has three attributes:
id
Attribute:
id
attribute has a value of "12345"
, which serves as a unique identifier for the product.category
Attribute:
category
attribute has a value of "Electronics"
, indicating the category to which the product belongs.brand
Attribute:
brand
attribute has a value of "Samsung"
, specifying the brand of the product.Attributes provide additional information about elements in XML. Here's a breakdown of attributes in XML:
=
) and the value enclosed in double quotes ("
).product[@id]
to access the id
attribute of the <product>
element.In the provided example, the attributes id
, category
, and brand
provide additional information about the product element, enhancing its semantics and allowing for more detailed description of the product.