


XML development basics-detailed code explanation of XML attributes
XML elements can contain the attribute in the opening tag, similar to HTML.
Attributes provide additional information about the element.
XML Attributes
From HTML, you will recall this: . The "src" attribute provides additional information about the
element.
In HTML (and in XML), attributes provide additional information about an element:
<img src="computer.gif"> <a href="demo.asp">
Attributes often provide information that is not part of the data. In the following example, the file type is irrelevant to the data, but is important to the software that needs to process this element:
<file type="gif">computer.gif</file>
XML attributes must be quoted
Attribute values must be surrounded by quotes , but both single and double quotes can be used. For example, for a person's gender, the person tag can be written like this:
<person sex="female">
or this way:
<person sex='female'>
Note: If the attribute value itself contains double quotes, it is necessary Surround it with single quotes, like this example:
<gangster name='George "Shotgun" Ziegler'>
Or you can use entity to quote :
<gangster name="George "Shotgun" Ziegler">
XML Elements vs. Attributes
See These examples:
<person sex="female">Anna Smith female Anna Smith
In the first example, sex is an attribute. In the second example, sex is a child element. Both examples provide the same information.
There are no rules that tell us when to use attributes and when to use subelements. My experience is that in HTML, attributes are very convenient to use, but in XML, you should try to avoid using attributes. If the information feels a lot like data, use child elements.
My favorite way
The following three XML documents contain the exact same information:
The first example uses the date attribute :
<note date="08/08/2008"> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget the meeting this weekend!</body> </note>
The second example uses the date element:
<note> <date>08/08/2008</date> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget the meeting this weekend!</body> </note>
The third example uses the extended date element (this is my favorite):
<note> <date> <day>08</day> <month>08</month> <year>2008</year> </date> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget the meeting this weekend!</body> </note>
Avoid XML attributes?
Some problems caused by using attributes:
Attributes cannot contain multiple values (subelements can)
Attributes cannot describe the tree structure (subelements can)
Attributes are not easy to extend (for future changes)
Attributes are difficult to read and maintain
Please try to use elements to describe data. Instead, just use attributes to provide data-independent information.
Don't do something stupid like this (this is not the way XML should be used):
<note day="08" month="08" year="2008" to="George" from="John" heading="Reminder" body="Don't forget the meeting this weekend!"> </note>
XML attributes for metadata
Sometimes ID references are assigned to elements. These ID indexes can be used to identify XML elements in the same way as the ID attribute in HTML. This example demonstrates this to us:
<messages> <note id="501"> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget the meeting this weekend!</body> </note> <note id="502"> <to>John</to> <from>George</from> <heading>Re: Reminder</heading> <body>I will not</body> </note> </messages>
The ID above is just an identifier, used to identify different notes. It is not part of the note data.
The idea we are trying to convey to you here is that metadata (data about data) should be stored as attributes, and the data itself should be stored as elements.
The above is the detailed content of XML development basics-detailed code explanation of XML attributes. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Can XML files be opened with PPT? XML, Extensible Markup Language (Extensible Markup Language), is a universal markup language that is widely used in data exchange and data storage. Compared with HTML, XML is more flexible and can define its own tags and data structures, making the storage and exchange of data more convenient and unified. PPT, or PowerPoint, is a software developed by Microsoft for creating presentations. It provides a comprehensive way of

The Gson@SerializedName annotation can be serialized to JSON and have the provided name value as its field name. This annotation can override any FieldNamingPolicy, including the default field naming policy that may have been set on the Gson instance. Different naming strategies can be set using the GsonBuilder class. Syntax@Retention(value=RUNTIME)@Target(value={FIELD,METHOD})public@interfaceSerializedNameExample importcom.google.gson.annotations.*;

Python's dir() function: View an object's properties and methods, specific code example required Summary: Python is a powerful and flexible programming language, and its built-in functions and tools provide developers with many convenient features. One of the very useful functions is the dir() function, which allows us to view the properties and methods of an object. This article will introduce the usage of the dir() function and demonstrate its functions and uses through specific code examples. Text: Python’s dir() function is a built-in function.

How to handle XML and JSON data formats in C# development requires specific code examples. In modern software development, XML and JSON are two widely used data formats. XML (Extensible Markup Language) is a markup language used to store and transmit data, while JSON (JavaScript Object Notation) is a lightweight data exchange format. In C# development, we often need to process and operate XML and JSON data. This article will focus on how to use C# to process these two data formats, and attach

Jackson is a Java-based library that is useful for converting Java objects to JSON and JSON to Java objects. JacksonAPI is faster than other APIs, requires less memory area, and is suitable for large objects. We use the writeValueAsString() method of the XmlMapper class to convert the POJO to XML format, and the corresponding POJO instance needs to be passed as a parameter to this method. Syntax publicStringwriteValueAsString(Objectvalue)throwsJsonProcessingExceptionExampleimp

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

If you want to get the pixels to which the document is scrolled from the upper left corner of the window, use the pageXoffset and pageYoffset properties. Use pageXoffset for horizontal pixels. Example You can try running the following code to learn how to use the pageXOffset attribute in JavaScript - Live Demonstration<!DOCTYPEhtml><html> <head> <style> &

Bottom attribute syntax and code examples in CSS In CSS, the bottom attribute is used to specify the distance between an element and the bottom of the container. It controls the position of an element relative to the bottom of its parent element. The syntax of the bottom attribute is as follows: element{bottom:value;} where element represents the element to which the style is to be applied, and value represents the bottom value to be set. value can be a specific length value, such as pixels
