Home Backend Development XML/RSS Tutorial Detailed introduction to attribute learning methods in XML

Detailed introduction to attribute learning methods in XML

Mar 10, 2017 pm 07:52 PM
xml Attributes

This article mainly introduces the attribute learning tutorial in XML, including examples of using attributes to store data in sub-elements. Friends in need can refer to it

Attributes are part of XML elements. An element can have multiple unique attributes. Attributes provide more information about an XML element. More precisely, they define the element's properties. An XML attribute is always a name-value pair.

Syntax
The XML attribute syntax is as follows:

<element-name attribute1 attribute2 >
....content..   
</element-name>
Copy after login

where attribute1 and attribute2 have the following form:

name = "value"
Copy after login

value must be wrapped in double quotes (" ") or single quotes (' '). Here attribute1 and attribute2 are both unique attribute labels.

Attributes are used to add a unique label to an element, a category label, add a Boolean attribute or associate some string data. The following example demonstrates how to use attributes:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE garden [   
    <!ELEMENT garden (plants)*>
    <!ELEMENT plants (#PCDATA)>
    <!ATTLIST plants category CDATA #REQUIRED>
]>
<garden>
<plants category="flowers" />
<plants category="shrubs">
</plants>
</garden>
Copy after login

Attributes are used to distinguish elements with the same name. When we don't want to create a new element for every case. We can use attributes to add more detail to differentiate between two or more similar elements.

In the above example, we have classified the plants by including the category attribute and assigned a different value to each element. So we have two plants categories, one is flowers and the other is color. This way we both get two plants elements with different attributes.

You can also see that we define this attribute at the beginning of the XML.

Attribute Type
The following table lists the attribute types:

Attribute TypeDescription
StringTypeAccepts a string value as the value. CDATA is a StringType. CDATA is also character data. This also means that any non-markup character is a legal attribute.
TokenizedType

This is a restricted type. Validity constraints indicated in the grammar are applied after attribute values ​​are normalized. The following is the TokenizedType attribute:

  • ID: is used to specify that the element is unique.

  • IDREF: is used to reference an ID that names another element.

  • IDREFS: All IDs used to reference an element.

  • ENTITY: Indicates that the attribute will represent an external entity in the document.

  • #ENTITYS: Indicates that the attribute will represent an external entity in the document.

  • NMTOKEN: Similar to CDATA that limits what data can be part of an attribute.

  • NMTOKENS: Similar to CDATA that limits what data can be part of an attribute.

EnumeratedType

Contains a predefined list of values ​​in its declaration. Here it must be assigned a value. There are two types of enumeration attributes:

  • NotationType: It declares that the element will be referenced as a NOTATION statement somewhere in the XML document.

  • Enumeration: Enumerations allow us to define a specific list of values ​​that an attribute value must match.


Element attribute rules
The following are the rules to follow when defining attributes:

The attribute name can only appear once in the same start tag or empty element tag.
Attributes must be defined in the document type definition (DTD) using the Attribute-List Declaration.
Attribute values ​​cannot directly or indirectly reference external entities.
The alternative text of any entity mentioned directly or indirectly in the attribute value cannot contain a less than sign (<).

Storing data in child elements
The date attribute is used in one case:

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

<heading>Reminder</heading>
<body>Don&#39;t forget me this weekend!</body>
</note>
Copy after login

It is used in the second case Date element:

<note>
<date>12/11/2002</date>

<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don&#39;t forget me this weekend!</body>
</note>
Copy after login

The extended date element is used in the third case (this is our common method):

<note>
<date>
  <day>12</day>
  <month>11</month>

  <year>2002</year>
</date>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>

<body>Don&#39;t forget me this weekend!</body>
</note>
Copy after login


The above is the detailed content of Detailed introduction to attribute learning methods in XML. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Can I open an XML file using PowerPoint? Can I open an XML file using PowerPoint? Feb 19, 2024 pm 09:06 PM

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

How to handle XML and JSON data formats in C# development How to handle XML and JSON data formats in C# development Oct 09, 2023 pm 06:15 PM

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

Python's dir() function: View the properties and methods of an object Python's dir() function: View the properties and methods of an object Nov 18, 2023 pm 01:45 PM

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.

Convert POJO to XML using Jackson library in Java? Convert POJO to XML using Jackson library in Java? Sep 18, 2023 pm 02:21 PM

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

bottom attribute syntax in CSS bottom attribute syntax in CSS Feb 21, 2024 pm 03:30 PM

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

How to use PHP functions to process XML data? How to use PHP functions to process XML data? May 05, 2024 am 09:15 AM

Use PHPXML functions to process XML data: Parse XML data: simplexml_load_file() and simplexml_load_string() load XML files or strings. Access XML data: Use the properties and methods of the SimpleXML object to obtain element names, attribute values, and subelements. Modify XML data: add new elements and attributes using the addChild() and addAttribute() methods. Serialized XML data: The asXML() method converts a SimpleXML object into an XML string. Practical example: parse product feed XML, extract product information, transform and store it into a database.

What is the role of pageXOffset attribute in JavaScript? What is the role of pageXOffset attribute in JavaScript? Sep 16, 2023 am 09:17 AM

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> &amp

Introduction to the attributes of Hearthstone's Despair Thread Introduction to the attributes of Hearthstone's Despair Thread Mar 20, 2024 pm 10:36 PM

Thread of Despair is a rare card in Blizzard Entertainment's masterpiece &quot;Hearthstone&quot; and has a chance to be obtained in the &quot;Wizbane's Workshop&quot; card pack. Can consume 100/400 arcane dust points to synthesize the normal/gold version. Introduction to the attributes of Hearthstone's Thread of Despair: It can be obtained in Wizbane's workshop card pack with a chance, or it can also be synthesized through arcane dust. Rarity: Rare Type: Spell Class: Death Knight Mana: 1 Effect: Gives all minions a Deathrattle: Deals 1 damage to all minions

See all articles