preface

Today I sorted out the knowledge points I had learned in the library and found myself making this learning record. Just very messy and very messy, now rearrange, record. After all, it was a period of time that I had learned by myself before

At that time was in the rookie tutorial for learning, also do not know for XML this piece, their arrangement is right. Look at it as an extension

XML profile

  1. XML is designed to transfer and store data.
  2. HTML is designed to display data.

What is XML?

  • XML stands for EXtensible Markup Language.
  • XML is a markup language much like HTML.
  • XML is designed to transmit data, not display it.
  • XML tags are not predefined. You need to define your own tags.
  • XML is designed to be self-descriptive.
  • XML is a W3C recommendation.

Differences between XML and HTML

XML is not a replacement for HTML.

HTML is designed to display information, while XML is designed to transmit information.

XML and HTML are designed for different purposes:

  1. XML is designed to transmit and store data, with the focus on the content of the data.
  2. HTML is designed to display data, and the focus is on what the data looks like.

XML doesn’t do anything

Maybe this is a little hard to understand, but XML doesn’t do anything. XML is designed to structure, store, and transmit information.

Here is an example of a note from Jani to Tove, stored as XML:

<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
Copy the code

The note above is self-descriptive. It contains information about the sender and receiver, as well as the title and the body of the message.

However, the XML document still doesn’t do anything. It’s just pure information wrapped in XML tags, and we need to write software or programs to send, receive, and display this document.

With XML you can invent your own tags

The tags in the example above are not defined in any XML standard (such as

and

).

These tags were invented by the creators of XML documents because the XML language has no predefined tags, and the tags used in HTML are predefined.

HTML documents can only use tags defined in the HTML standard (such as

, < H1 >, and so on), and XML allows authors to define their own tags and their own document structure.

XML is not a replacement for HTML

XML is a complement to HTML.

It is important to understand that XML is not a replacement for HTML; in most Web applications, XML is used to transfer data, while HTML is used to format and display data.

XML is best described as a software – and hardware-independent tool for conveying information.

XML is a W3C recommendation

XML became a W3C recommendation on February 10, 1998.

XML is everywhere

Today, XML plays as much of a role in the Web as HTML has always been the cornerstone of the Web, and XML is the most common tool for transferring data between applications.

XML USES

XML is used in many aspects of Web development, often to simplify data storage and sharing.

XML separates data from HTML

If you need to display dynamic data in an HTML document, you will spend a lot of time editing the HTML whenever the data changes.

With XML, data can be stored in a separate XML file. This allows you to focus on display and layout using HTML/CSS and ensure that no HTML changes are required to modify the underlying data.

With a few lines of JavaScript code, you can read an external XML file and update the data content of your web page.

XML simplifies data sharing

In the real world, computer systems and data use incompatible formats to store data.

XML data is stored in plain text format, thus providing a software – and hardware-independent method of data storage.

This makes it easier to create data that different applications can share.

XML simplifies data transfer

One of the most time-consuming challenges for developers has been exchanging data between incompatible systems on the Internet.

Exchanging data in XML reduces this complexity because it can be read through a variety of incompatible applications.

XML simplifies platform changes

Upgrading to a new system (hardware or software platform) is always very time consuming. Large amounts of data must be converted, and incompatible data is often lost. XML data is stored in text format, which makes it easier to extend or upgrade XML to new operating systems, new applications, or new browsers without losing data.

XML makes your data more useful

Different applications can access your data, not only in HTML pages, but also from XML data sources.

With XML, your data can be used by a variety of reading devices (handheld computers, voice devices, news readers, and so on), as well as by people who are blind or otherwise disabled.

XML is used to create new Internet languages

Many new Internet languages are created through XML.

Here are some examples:

  • XHTML
  • WSDL for describing the available Web services
  • WAP and WML as markup languages for handheld devices
  • RSS language for news feeds
  • RDF and OWL for describing capital and ontology
  • SMIL for describing multimedia for the Web

Let’s say developers are rational

If they are rational, let future applications use XML to exchange data.

In the future there may be word processors, spreadsheet programs, and databases that can read each other’s data in XML format without the need for any converters.

The XML tree structure

The XML document forms a tree structure that starts at the “roots” and extends to the “branches and leaves.”

An XML document instance

XML documents use a simple self-descriptive syntax:


      
<note>
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Don't forget me this weekend!</body>
</note>
Copy the code

: XML declaration, which defines the version of XML (1.0) and the encoding used.

: Describes the root element of the document (as in, “This document is a post-it note”)

< to > < to > < the from > < / from > < heading > < / heading > < body > < / body > : four word elements

You can assume that from this example, the XML document contains a note from Jani to Tove.

As a result, XML is remarkably self-descriptive.

XML documents form a tree structure

<root>
    <child>
        <subchild>.</subchild>
    </child>
</root>
Copy the code

The XML document must contain the root element. This element is the parent of all other elements. Elements in an XML document form a document tree. The tree starts at the root and spreads to the very bottom of the tree. Terms such as parent, child, and sibling are used to describe relationships between elements. Parent element has child element. Child elements at the same level become siblings (brothers or sisters), and all elements can have textual content and attributes (similar to HTML).

The following figure shows a book in the XML below:

XML document instance

<bookstore>
	<book category="COOKING">
		<title lang="en">Everyday Italian</title>
		<author>Giada De Laurentiis</author>
		<year>2005</year>
		<price>30.00</price>
	</book>
	<book category="CHILDREN">
		<title lang="en">Harry Potter</title>
		<author>J K. Rowling</author>
		<year>2005</year>
		<price>29.99</price>
	</book>
	<book category="WEB">
		<title lang="en">Learning XML</title>
		<author>Erik T. Ray</author>
		<year>2003</year>
		<price>39.95</price>
	</book>
</bookstore>
Copy the code
  • The root element in the instance is.
  • All elements in the document are contained in.
  • The element has four child elements:,,, and.

XML syntax rules

The syntax rules for XML are simple and logical. The rules are easy to learn and easy to use.

XML documents must have a root element

XML must contain the root element, which is the parent of all other elements,

For example, root is the root element in the following example:

<root>
 <child>
   <subchild>.</subchild>
 </child>
</root>
Copy the code

Note is the root element in the following example:


      
<note>
 <to>Tove</to>
 <from>Jani</from>
 <heading>Reminder</heading>
 <body>Don't forget me this weekend!</body>
</note>
Copy the code

The XML declaration

The optional part of the XML declaration file, if any, needs to be placed in the first line of the document, as follows:


      
Copy the code

The examples above include the XML version, and UTF-8 is also the default encoding for HTML5, CSS, JavaScript, PHP, and SQL.

XML elements must have a close tag

In HTML, certain elements do not have to have a closing tag. Common examples are:

<br>,<hr>,<img>,<input>,<link>,<meta>
Copy the code

In XML, it is illegal to omit the closing tag. All elements must have a close tag

Note: From the example above, you may have noticed that the XML declaration does not have a closing tag. This is not a mistake. The declaration is not part of the XML document itself, and it does not have a closing tag.

XML tags are case-sensitive

HTML tags are insensitive, while XML tags are case-sensitive. Labels are different from labels. Open and close tags must be written in the same case:

<Message>This is wrong</message>
<message>This is correct</message>
Copy the code

Note: Open and close tabs are often referred to as opening and closing tabs. Whichever term you prefer, the concept is the same.

XML attribute values must be quoted

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

In XML, attribute values of XML must be quoted.

In the following two XML documents, the first is incorrect and the second is correct:

The error in the first document is that the date attribute in the note element is not quoted.

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

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

Entity references

In XML, some characters have special meaning.

If you place the character “<” inside an XML element, an error occurs because the parser treats it as the start of a new element.

This produces an XML error:

<message>if salary < 1000 then</message>
Copy the code

To avoid this error, replace the “<” character with an entity reference:

<message>if salary &lt; 1000 then</message>
Copy the code

In XML, there are five predefined entity references:

< < less than
> > greater than
& & ampersand
apostrophe
quotation mark

Note: Only the characters “<” and “&” are actually illegal in XML. The greater-than sign is legal, but it’s a good habit to replace it with an entity reference.

Annotations in XML

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

<! -- This is a comment -->
Copy the code

In XML, whitespace is preserved and not trimmed. HTML reduces (merges) consecutive space characters into one.

XML stores newlines in LF

  • In Windows applications, newlines are typically stored as a pair of characters: carriage return (CR) and newline (LF).
  • In Unix and Mac OSX, LF is used to store new rows.
  • On older Mac systems, CR was used to store new rows.
  • Store line feeds in XML as LF.

XML elements

XML documents contain XML elements.

What is an XML element?

An XML element is the part that runs from (and includes) the start tag to (and includes) the end tag.

An element can contain:

  • Other elements
  • The text
  • attribute
  • Or a mix of all of the above…
<bookstore>
    <book category="CHILDREN">
        <title>Harry Potter</title>
        <author>J K. Rowling</author>
        <year>2005</year>
        <price>29.99</price>
    </book>
    <book category="WEB">
        <title>Learning XML</title>
        <author>Erik T. Ray</author>
        <year>2003</year>
        <price>39.95</price>
    </book>
</bookstore>
Copy the code

XML naming rules

XML elements must follow the following naming rules:

  • A name can contain letters, numbers, and other characters
  • Names cannot start with numbers or punctuation marks
  • Names cannot start with the letter XML (or XML, XML, and so on)
  • The name cannot contain Spaces

Use any name, no reserved words.

Best naming conventions

  1. Make the name descriptive.
  2. It’s also good to use underscore names: <first_name>, <last_name>.
    1. Names should be short and simple, such as

      , not .
  3. Avoid the “-” character.
    1. If you name it like this: “first-name”, some software will think you want to subtract the name from first.
  4. Avoid the “.” character.
    1. If you name it like this: “first.name”, some software will assume that “name” is a property of the object “first”.
  5. Avoid the “:” character. The colon is converted to a namespace for use (more on that later).
  6. XML documents often have a database with fields that correspond to elements in the XML document.
  7. A practical rule of thumb is to use database naming conventions to name elements in XML documents.
  8. In XML, non-English letters like EOA are perfectly legal,
  9. Be aware, however, of problems that may arise if your software vendor does not support these characters.

XML elements are extensible

XML elements are extensible to carry more information.

Take a look at the following XML example:

<note>
   <to>Tove</to>
   <from>Jani</from>
   <body>Don't forget me this weekend!</body>
</note>
Copy the code

Imagine that the author of an XML document adds some additional information:

<note>
   <date>2008-01-10</date>
   <to>Tove</to>
   <from>Jani</from>
   <heading>Reminder</heading>
   <body>Don't forget me this weekend!</body>
</note>
Copy the code

An application can find, and elements in an XML document and produce the same output.

One of the advantages of XML is that it can be extended without breaking the application.

XML attributes

  • XML elements have attributes, similar to HTML.
  • Attributes provide additional information about elements.

XML attributes must be quoted

Attribute values must be enclosed in quotation marks, although both single and double quotation marks can be used.

For example, the person element could be written like this:

<person sex="female">
Copy the code

Or this could work:

<person sex='female'>
Copy the code

You can use single quotes if the attribute value itself contains double quotes

<gangster name='George "Shotgun" Ziegler'>
Copy the code

Or you can use character entities:

<gangster name="George &quot;Shotgun&quot; Ziegler">
Copy the code

XML elements vs attributes

Look at these examples:

<person sex="female">
  <firstname>Anna</firstname>
  <lastname>Smith</lastname>
</person>


<person>
  <sex>female</sex>
  <firstname>Anna</firstname>
  <lastname>Smith</lastname>
</person>
Copy the code

In the first instance, sex is an attribute.

In the second instance, sex is an element. Both instances provide the same information.

There are no rules that tell us when to use attributes and when to use elements.

My experience is that in HTML, attributes are handy to use, but in XML, you should try to avoid using attributes.

If information feels like data, use elements instead.

Avoid XML attributes

Properties are difficult to read and maintain. Try to use elements to describe data, and only attributes to provide information unrelated to data.

Some of the problems caused by using attributes:

  • Attributes cannot contain more than one value (elements can)
  • Attributes cannot contain tree structures (elements can)
  • Properties not easily extended (for future changes)

Don’t be silly (this is not how XML should be used) :

<note day="10" month="01" year="2008" to="Tove" from="Jani" heading="Reminder" body="Don't forget me this weekend!"></note>
Copy the code

XML attributes for metadata

Elements are sometimes assigned ID references, and these ID indexes can be used to identify XML elements in the same way as ID attributes in HTML.

This example demonstrates the situation:

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

  <note id="502">
    <to>Jani</to>
    <from>Tove</from>
    <heading>Re: Reminder</heading>
    <body>I will not</body>
  </note>
</messages>
Copy the code

The ID attribute above is simply an identifier used to identify the different notes and is not part of the note data.

Here we are trying to convey to you the idea is:

Metadata (data about data) should be stored as attributes, while the data itself should be stored as elements.

XML validation

XML with correct syntax is called “well-formed” XML.

XML validated by a DTD is “legitimate” XML.

Well-formed XML documents

A “well-formed” XML document has correct syntax.

The syntax rules described in the previous section:

  • XML documents must have a root element
  • XML elements must have a close tag
  • XML tags are case-sensitive
  • XML elements must be properly nested
  • XML attribute values must be quoted

      
<note>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>
Copy the code

Validating XML documents

A legitimate XML document is a “well-formed” XML document, which also conforms to the rules of the Document Type Definition (DTD) :


      
<! DOCTYPEnote SYSTEM "Note.dtd">
<note>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>
Copy the code

In the above example, the DOCTYPE declaration is a reference to an external DTD file.

XML DTD

The purpose of DTDS is to define the structure of XML documents. It uses a set of legal elements to define the document structure:

<! DOCTYPE note [ <!ELEMENT note (to,from,heading,body)> <!ELEMENT to (#PCDATA)> <!ELEMENT from (#PCDATA)> <!ELEMENT heading (#PCDATA)> <!ELEMENT body (#PCDATA)> ]>Copy the code

The XMLHttpRequest object

The XMLHttpRequest object is used to exchange data with the server in the background.

The XMLHttpRequest object is a developer’s dream because you can:

  • Update a web page without reloading the page
  • Request data from the server after the page has loaded
  • Receives data from the server after the page has been loaded
  • Sends data to the server in the background

Create an XMLHttpRequest object

All modern browsers (IE7+, Firefox, Chrome, Safari, and Opera) have built-in XMLHttpRequest objects.

Syntax for creating an XMLHttpRequest object:

xmlHttp=new XMLHttpRequest();

ActiveX objects are used in older versions of Internet Explorer (IE5 and IE6) :

xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");

The XML parser

All modern browsers have a built-in XML parser.

XML parsers convert XML documents into XML DOM objects – objects that can be manipulated through JavaScript.

Parsing XML documents

The following code snippet parses an XML document into an XML DOM object:

if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
	xmlhttp=new XMLHttpRequest();
} else	{// code for IE6, IE5
	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

xmlhttp.open("GET"."books.xml".false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
Copy the code

Parsing XML Strings

The following code snippet parses an XML string into an XML DOM object:

txt="<bookstore><book>";
txt=txt+"<title>Everyday Italian</title>";
txt=txt+"<author>Giada De Laurentiis</author>";
txt=txt+"<year>2005</year>";
txt=txt+"</book></bookstore>";

if (window.DOMParser){
	parser=new DOMParser();
	xmlDoc=parser.parseFromString(txt,"text/xml");
} else { // Internet Explorer
  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  xmlDoc.async=false;
  xmlDoc.loadXML(txt); 
}
Copy the code

**Internet Explorer uses the loadXML() method to parse XML strings

Other browsers use DOMParser objects.

Cross domain access

For security reasons, modern browsers do not allow cross-domain access.

This means that both the web page and the XML file it is trying to load must be on the same server.

XML namespace

XML namespaces provide a way to avoid element naming conflicts.

Naming conflicts

In XML, element names are defined by the developer, and naming conflicts occur when two different documents use the same element name.

This XML carries information about the HTML table:

<table>
  <tr>
  <td>Apples</td>
  <td>Bananas</td>
  </tr>
</table>
Copy the code

This XML document carries information about the table (a piece of furniture) :

<table>
  <name>African Coffee Table</name>
  <width>80</width>
  <length>120</length>
</table>
Copy the code

If these two XML documents are used together, since both documents contain different contents and definitions

Use prefixes to avoid naming conflicts

Naming conflicts in XML can be easily avoided by using name prefixes.

This XML carries information about an HTML table and a piece of furniture:

<h:table>
  <h:tr>
    <h:td>Apples</h:td>
    <h:td>Bananas</h:td>
  </h:tr>
</h:table>

<f:table>
  <f:name>African Coffee Table</f:name>
  <f:width>80</f:width>
  <f:length>120</f:length>
</f:table>
Copy the code

In the above example, there would be no conflict because of the two

XML namespace – XMLNS attribute

When prefixes are used in XML, a so-called namespace for the prefix must be defined in the XMLNS attribute of the element’s start tag.

The syntax for namespace declarations is as follows. XMLNS: prefix =”URI”.

<root>
  <h:table xmlns:h="http://www.w3.org/TR/html4/">
    <h:tr>
      <h:td>Apples</h:td>
      <h:td>Bananas</h:td>
    </h:tr>
  </h:table>

  <f:table xmlns:f="http://www.w3cschool.cc/furniture">
    <f:name>African Coffee Table</f:name>
    <f:width>80</f:width>
    <f:length>120</f:length>
  </f:table>
</root>
Copy the code

In the example above,

When a namespace is defined in the opening tag of an element, all child elements with the same prefix are associated with the same namespace.

Namespaces, which can be declared in the element they are used or in the XML root element:

<root支那xmlns:h="http://www.w3.org/TR/html4/" xmlns:f="http://www.w3cschool.cc/furniture">
  <h:table>
    <h:tr>
      <h:td>Apples</h:td>
      <h:td>Bananas</h:td>
    </h:tr>
  </h:table>

  <f:table>
  <f:name>African Coffee Table</f:name>
  <f:width>80</f:width>
  <f:length>120</f:length>
  </f:table>
</root>
Copy the code

** Namespace URIs are not used by parsers to find information; their purpose is to give the namespace a unique name. However, many companies often use namespaces as Pointers to an actual web page that contains information about the namespace.

The default namespace

Defining a default namespace for an element saves us the work of using prefixes in all child elements

Its syntax is as follows: XMLNS =”*namespaceURI*”

This XML carries information about the HTML table:

<table xmlns="http://www.w3.org/TR/html4/">
	<tr>
    <td>Apples</td>
    <td>Bananas</td>
  </tr>
</table>
Copy the code

Namespaces in real use

XSLT is an XML language for transforming XML documents into other formats, such as HTML.

In the XSLT document below, you can see that most tags are HTML tags, and non-HTML tags are prefixed with XSL and identified by this namespace:

xmlns:xsl="http://www.w3.org/1999/XSL/Transform"


      
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <html>
      <body>
      	<h2>My CD Collection</h2>
        <table border="1">
          <tr>
            <th align="left">Title</th>
            <th align="left">Artist</th>
          </tr>
          <xsl:for-each select="catalog/cd">
            <tr>
              <td><xsl:value-of select="title"/></td>
              <td><xsl:value-of select="artist"/></td>
            </tr>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>
Copy the code

DTD constrained documents

  • There is also a
  • <! DOCTYPE web - the app SYSTEM "web - app_3_3. DTD" >
    • Import the label in the target document to constrain
  • <! ELEMENT tag type name >
    • Declare that label type names can appear in XML documents
  • <! ATTLIST Tag type name Attribute name >
    • Declares the name of an attribute that can be used within the current tag
  • <! ELEMENT tag type name (child tag name)>
    • The tag must appear inside the parent tag only once
  • <! ELEMENT Tag type name >
  • The child tag may or may not appear inside the parent tag
  • The child tag, if present, can only appear once
  • <! ELEMENT Tag type name (subtag name *)>
    • The child tag may or may not appear inside the parent tag
    • Sublabels can appear more than once
  • <! ELEMENT tag type name (subtag name +)>
    • The child tag must appear inside the parent tag and can appear more than once
  • <! ELEMENT type name tag ((a tag name | child tags 2)) >
    • Only one of the two child tags must appear inside the parent tag
  • <! ELEMENT Tag type name (subtag name 1, subtag name 2)>
    • The child tag must appear inside the parent tag only once
    • And you want to do it in elder brother order so child tag 1 comes before child tag 2
  • <! ELEMENT tag type name (#PCDATA)>
    • Tags end with a declaration that they have no child tags
    • The current label has no child labels

Attribute definitions

<! ATTLIST Element name Attribute Name Attribute Type Setting >

There are four Settings

  1. #REQUIRED indicates that the attribute of the element is REQUIRED
  2. #IMPLIED indicates that the element may or may not contain the property
  3. #FIXED indicates a FIXED default value for the property. When used, you need to provide a default value that is immutable
  4. Default If the element does not contain the attribute, it is automatically set to the default value, but the default value can be changed

After the speech

I don’t know. I’m still looking for an internship. Hope to have a summer internship to see my brother WWWWu.