search
  • Sign In
  • Sign Up
Password reset successful

Follow the proiects vou are interested in andi aet the latestnews about them taster

Home Backend Development XML/RSS Tutorial XML file structure and basic syntax

XML file structure and basic syntax

Dec 19, 2016 pm 04:08 PM

XML文件结构和基本语法

分类: html java2010-03-12 17:04 903人阅读 评论(1) 收藏 举报

4.1.1               XML文件结构

一个XML文件通常包含文件头和文件体两大部分

1.         文件头

XML文件头由XML声明与DTD文件类型声明组成。其中DTD文件类型声明是可以缺少的,关于DTD声明将在后续的内容中介绍,而XML声明是必须要有的,以使文件符合XML的标准规格。

在前面的Flowers.xml文件中的第一行代码即为XML声明:

<?xml version="1.0" encoding="gb2312"?>

              其中:

“<?”代表一条指令的开始,“?>”代表一条指令的结束;

“xml”代表此文件是XML文件;

“ version="1.0" ”代表此文件用的是XML1.0标准;

“ encoding="gb2312" ” 代表此文件所用的字符集,默认值为Unicode,如果该文件中要用到中文,就必须将此值设定为gb2312。

 

注意:XML声明必须出现在文档的第一行。

 

2.         文件体

文件体中包含的是XML文件的内容,XML元素是XML文件内容的基本单元。从语法讲,一个元素包含一个起始标记、一个结束标记以及标记之间的数据内容。

 

XML元素与HTML元素的格式基本相同,其格式如下:

<标记名称 属性名1="属性值1" 属性名1="属性值1" ……>内容</标记名称>

 

所有的数据内容都必须在某个标记的开始和结束标记内,而每个标记又必须包含在另一个标记的开始与结束标记内,形成嵌套式的分布,只有最外层的标记不必被其他的标记所包含。最外层的是根元素(Root),又称文件(Document)元素,所有的元素都包含在根元素内。

在前面的Flowers.xml文件中,根元素就是<Flowers>,根元素必须而且只能有一个,在该文件有三个<Flower>子元素,这样的元素可以有多个。

4.1.2               XML的基本语法

1.         注释

XML的注释与HTML的注释相同,以“<!--”开始,以“-->”结束。

 

2.         区分大小写

在HTML中是不区分大小写的,而XML区分大小写,包括标记,属性,指令等。

 

3.         标记

XML标记与HTML标记相同,“<”表示一个标记的开始,“>” 表示一个标记的结束。XML中只要有起始标记,就必须有结束标记,而且在使用嵌套结构时,标记之间不能交叉。

在XML中不含任何内容的标记叫做空标记,格式为:<标记名称/>

 

4.         属性

XML属性的使用与HTML属性基本相同,但需要注意的是属性值要加双引号。

 

5.         实体引用

实体引用是指分析文档时会被字符数据取代的元素,实体引用用于XML文档中的特殊字符,否则这些字符会被解释为元素的组成部分。例如,如果要显示“<”,需要使用实体引用“&lt;”否则会被解释为一个标记的起始。

XML中有5个预定义的实体引用,如表 4.1所示。

表 4.1 XML预定义的实体引用

6. CDATA

在XML中由一个特殊的标记CDATA,在CDATA中所有文本都不会被XML处理器解释,直接显示在浏览器中,使用方法如下:

<![CDATA[

这里的内容可以直接显示。

]]>

 

7.         处理指令

处理指令使用来给处理XML文件的应用程序提供信息的,处理指令的格式如下:

<?处理指令名称 处理指令信息?>

              例如,XML声明就是一条处理指令:

<?xml version="1.0" encoding="gb2312"?>

Among them, "xml" is the processing instruction name, version="1.0" encoding="gb2312" is the processing instruction information.

4.2 XML and CSS

CSS can be used to set the display mode of XML files, that is, add the following statement at the head of the XML file, below the XML declaration:

<?xml:stylesheet type="text /css" href="URL of css file"?>

Let’s use examples to introduce how to use CSS to display XML files. First create a css file, the code is as follows:

flowers.css: A CSS style that displays XML files

                                                                                                    using   using   using       using         using using         through through out through out through out through out through out through ‐ ‐ toward ‐ ‐‐ ‐ , and that the code is as follows:

flowers.css red}

price{display:block}

Then, use this css style in the flowers.xml file, that is, add the following statement below the XML declaration in the flowers.xml file:

<?xml:stylesheet type="text/css" href="flowers.css"?>

The complete program code is as follows:

ex4_3_01.xml: Use CSS to display XML files

<?xml version="1.0" encoding < Vendor>shop1</Vendor>

                                                                                                                                                                                                       lower>

                                                            ;/Vendor>

                                                                                                                                                                              

                                 <Vendor>shop3</Vendor> ;

                                                                                          

                How this example appears in the browser The effect is shown in Figure 4.2.

Figure 4.2 Using CSS to display XML files

When using CSS to display XML files, there is no selectivity. That is to say, all data under the root element will be displayed, and the structure and structure of the original file cannot be changed. The order of content. In addition, CSS does not support Chinese tags because CSS is not a style language specifically developed for XML, but the XSL introduced below will do. XSL is specially designed for XML and is more complex than CSS.

4.3 XSL

The effect of using CSS to display XML files introduced in the previous section is not very satisfactory. In fact, CSS is mainly suitable for HTML files. For data files such as XML, CSS is not suitable, especially for those When the file contents need to be rearranged and displayed according to different conditions, XSL can be used.

XSL is currently the most powerful and flexible style language. It is specially designed for applying XML. It is much more complex than CSS. However, it is currently not well supported and not yet finalized.

Use XSL to set the display mode of the XML file, that is, add the following statement to the head of the XML file, below the XML declaration:

  <?xml:stylesheet type="text/xsl" href="xsl file URL"?>

Below we use examples to introduce how to use XSL to display XML files. First create an xsl file, the code is as follows:

flowers.xsl: xsl file used to display XML files

<?xml version="1.0" encoding="gb2312"?>

<xsl:stylesheet xmlns :xsl="http://www.w3.org/TR/WD-xsl">

<xsl:template match = "/">

  <table border="1">

<tr align="center">

                                                                                                

                                                                                                                     sl:for-each select ="//Flowers/Flower"> <td><xsl:value-of select="Name"/></td>

                                                            

                                                                                            

We can watch Output: An XSL file is just like an empty HTML file, and a traditional HTML file is produced by filling in an XML file.

An XSL file must first have an XML declaration (i.e. the first line), because XSL is actually a special XML file. The root element of XSL is xsl:stylesheet, that is, an XSL document must start with the <xsl:stylesheet> tag and end with the </xsl:stylesheet> tag. The xmlns:xsl attribute is used to set the XSL name domain.

The root element of XSL is usually composed of one or more template elements. In this example, it only contains a single template. It starts with the <xsl:template> tag and ends with the <xsl:template> tag. Use The match attribute can select qualified nodes in the XML file, that is, set the template name. For the top-level template, match is set to "/", which represents the root element of the entire XML file.

Then get the required data from the XML file. The easiest way to get the data is:

<xsl:value-of select="mode"/>

If you want to get multiple elements, you need to Use the xsl:for-each element in the following format:

<xsl:for-each select="mode">

…..

</xsl:for-each>

Connect Next, we need to use this xsl file in the flowers.xml file, that is, add the following statement under the XML declaration in the flowers.xml file:

<?xml:stylesheet type="text/xsl" href="flowers. xsl"?>

The complete program code is as follows:

ex4_4_01.xml: Using XSL to display XML files

<?xml version="1.0" encoding="gb2312"?>

<? xml:stylesheet type="text/xsl" href="flowers.xsl"?>

<Flowers>

<Flower> <Name> ;iris</Name> ;

                                                                        /Name>

              <Price>$4.30</Price>

</Flower>

                                                                                 

           <Price>$3.50</Price>

</Flower>

</Flowers>

For more articles related to XML file structure and basic syntax, please pay attention 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

Undress AI Tool

Undress AI Tool

Undress images for free

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

ArtGPT

ArtGPT

AI image generator for creative art from text prompts.

Stock Market GPT

Stock Market GPT

AI powered investment research for smarter decisions

Popular tool

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)

How to parse XML in Ruby on Rails? (Web Frameworks) How to parse XML in Ruby on Rails? (Web Frameworks) Jan 05, 2026 am 12:53 AM

UseNokogiriforfast,robustXMLparsinginRails:installviagem'nokogiri',parsewithNokogiri::XML,handleencoding/namespacesexplicitly,andextractdatasafelyusingcss()/xpath()whilecheckingdoc.errorsand.textguards.

How to compare two XML files and find differences How to compare two XML files and find differences Jan 05, 2026 am 01:05 AM

Use tools or programming methods to compare XML file differences: first use online tools (such as xmlcompare.org) or IDE plug-ins for visual comparison, and then implement automated analysis through the command line (such as xmllint diff) or Python scripts (using the lxml library to parse and standardize XML). Pay attention to the processing of whitespace characters, attribute order, namespace prefixes and other influencing factors. It is recommended to use CanonicalXML to eliminate format differences.

How to fix XML encoding issues (UTF-8)? (Character Sets) How to fix XML encoding issues (UTF-8)? (Character Sets) Jan 03, 2026 am 02:48 AM

XMLshowsgarbledtextwhendeclaredUTF-8encodingmismatchesactualbyteencoding;verifywithfile-iorxxd,fixbyresavingcorrectly,andparseinbinarymodetohonortheXMLdeclaration.

The Role of XML in modern configuration files (e.g., pom.xml, web.config) The Role of XML in modern configuration files (e.g., pom.xml, web.config) Jan 02, 2026 am 12:59 AM

XMLremainswidelyusedinmodernconfigurationfilesduetoitsstructured,hierarchicaldatarepresentation,enablingclearparent-childrelationshipsandmetadatastorageviatagsandattributes,asseeninpom.xmlandweb.config.2.ItsupportsformalvalidationthroughXSDandDTDsche

How to serialize C# objects to XML? (Data Persistence) How to serialize C# objects to XML? (Data Persistence) Jan 04, 2026 am 01:41 AM

XmlSerializerisidealforsimple,attribute-drivenXMLserializationinC#,requiringpublicproperties,parameterlessconstructors,andconcretetypes;itignores[Serializable],supportsListbutnotDictionary,lackscircularreferencehandling,andneedsexplicitnull/namespace

How to manage dependencies with a pom.xml in Maven How to manage dependencies with a pom.xml in Maven Jan 03, 2026 am 01:42 AM

The pom.xml file declares dependencies through groupId, artifactId and version, and can use scope to specify the scope; 2. Multi-module projects use dependencyManagement to unify versions; 3. Maven automatically handles transitive dependencies, and exclusions can be used to exclude conflicting libraries; 4. Use MavenVersionsPlugin to check dependency updates to keep projects safe and consistent.

How to delete specific nodes from XML in Java? (DOM Manipulation) How to delete specific nodes from XML in Java? (DOM Manipulation) Jan 07, 2026 am 12:14 AM

To delete a specific node, you need to operate based on the tag name and conditions (such as attribute values ​​or text content): the liveNodeList must be traversed in reverse order and removeChild() is called; XPath is recommended for complex conditions; be sure to check that the parent node is not empty before deleting to avoid NPE; performance and memory limitations should be considered for large-scale deletions.

How to deserialize XML into a C# object How to deserialize XML into a C# object Jan 02, 2026 am 12:45 AM

Use the XmlSerializer class to deserialize XML into a C# object. You need to define a class that matches the XML structure and annotate it with attributes such as XmlElement and XmlAttribute. Then read the XML stream and convert it into an object instance through the Deserialize method.

Related articles