The XML file format has been around for a long time. His popularity has now become less strong under the impact of emerging file formats such as JSON and YAML. But XML remains the most widely used file format in the world today. There are also a lot of concepts and knowledge points surrounding it. So it is still necessary for us to have a comprehensive understanding.
XMLXML stands for eXtensible Markup Language, which is an extensible markup language. It is designed to transmit and store data.
XML and HTML may seem similar, but their design purposes are different.
XML is used to transmit and store data, mainly focusing on what the data is.
HTML is used to display data, mainly focusing on what the data looks like.
HTML tags are predefined, such as the table tag, and the browser will know what it means.
XML tags are not predefined. You need to design the tag yourself and describe the meaning of the tag. If the tag in XML does not use an XSLT file, the browser will only display it in a simple text format.
Many people think that HTML is a subset of XML files. In fact, this view is wrong, because the implementation of HTML does not strictly follow the syntax of XML. For example, XML requires that each tag must have a closing tag, XML tags are case-sensitive, and attributes added to tags in XML must be enclosed in quotation marks... HTML does not meet these syntax requirements.
See an example of XML.
book.xml
1234567 Copy after login Copy after login | <?xml version="1.0" encoding="ISO-8859-1"?> <book> <name>Effective JavaScript</name> <category>Program Language</category> <author>Bowen</author> <description>This book is about JavaScript Language.</description> </book> Copy after login |
This is a simple XML file. The first line describes the xml version and encoding type. Next is a root node book, which can contain many child nodes.
XML namespaceSince XML tags are not predefined like HTML, it is very likely that tags with the same name in two XMLs have different meanings. Then conflicts will inevitably occur when merging XML and other operations. The solution is to add a namespace (i.e. namespace) to the XML tag. Each namespace can specify a prefix. These prefixes distinguish tags with the same name.
Suppose there is another xml file here.
anotherBook.xml
123456 Copy after login | <?xml version="1.0" encoding="ISO-8859-1"?> <book> <name>Rework</name> <page>120</page> <publishDate>2013-10-08</publishDate> </book> Copy after login |
If we want to merge these two xml nodes into the same xml file, there will be a conflict without adding namespace, because it contains a tag with the same name, and its child The structure of the nodes is not the same. Next we namespace it and merge it.
combined.xml
1234567891011121314 Copy after login | <root> <ns1:book xmlns:ns1="http://www.huangbowen.net/ns1"> <ns1:name>Effective JavaScript</name> <ns1:category>Program Language</category> <ns1:author>Bowen</author> <ns1:description>This book is about JavaScript Language.</description> </book> <ns2:book xmlns:ns2="http://www.huangbowen.net/ns2"> <ns2:name>Rework</name> <ns2:page>120</page> <ns2:publishDate>2013-10-08</publishDate> </book> </root> Copy after login |
xmlns is the abbreviation of xml namespace. Following the quotation marks is the prefix of the tag. This prefix can be omitted, such as xmlns="http://www.huangbowen.net/ns1", which is equivalent to automatically applying the default namespace to tags without a prefix. It should be noted that the URI of the namespace only provides a unique identifier for the namespace. The xml parser will not access this URI to obtain any information. Many companies are accustomed to using this URI as a web page, which describes the relevant information of the namespace.
XSDXSD stands for XML Schema Definition, which is XML structure definition language. Each XSD file is a structural definition of an XML file. Since tags in XML are not predefined, everyone can create their own XML structure document. If you want others to create an xml file according to your standards, you can use an XSD file to describe your standards.
This is an XSD file for the example book.xml file in this article.
book.xsd
12345678910111213 Copy after login | <xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="book"> <xs:complexType> <xs:sequence> <xs:element type="xs:string" name="name"/> <xs:element type="xs:string" name="category"/> <xs:element type="xs:string" name="author"/> <xs:element type="xs:string" name="description"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> Copy after login |
As you can see from the above, the XSD file itself is an XML file. It follows XML syntax. For example, each tag needs to have a closing tag, and it must have and There is only one root node and so on.
You can add its Schema reference information in an XML file.
book.xml
1234567 Copy after login Copy after login | <?xml version="1.0" encoding="ISO-8859-1"?> <ns1:book xmlns:ns1="http://www.huangbowen.net/ns1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:targetLocation="http://www.huangbowen.net/book.xsd"> <ns1:name>Effective JavaScript</name> <ns1:category>Program Language</category> <ns1:author>Bowen</author> <ns1:description>This book is about JavaScript Language.</description> </book> Copy after login |
在IDE中,如果你的XML节点没有遵守你引用的Schema中的定义,就会给出错误提醒。
XSLTXSLT全称为EXtensible Stylesheet Language Transformations。 XSLT用于将XML文档转换为XHTML或其他XML文档。
在讲XSLT之前我们先讲讲XSL。XSL全称为Extensible Stylesheet Language,即可扩展样式表语言。众所周知,CSS是HTML文件的样式表,而XSL则是XML文件的样式表。XSL文件描述了XML文件应该如何被显示。
其实XSL不仅仅是样式表语言,它主要包含3部分:
XSLT - 用来转换XML文档
XPath - 查询和操作XML文档中的节点
XSL-FO - 格式化XML文档
XSLT使用XPath来查找XML中的元素。
XSLT通过一个xml文件来定义源xml文件与目标文件之间的转换关系。该xml文件必须以
对于本文的示例book.xml,如果我们使用浏览器打开显示效果如下。
现在我们创建一个XSLT文件将其转换为一个HTML文件。
book.xsl
12345678910111213141516171819202122232425262728293031 Copy after login | <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h2>My Book</h2> <table border="1"> <tr> <td>name</td> <td><xsl:value-of select="book/name" /></td> </tr> <tr> <td>category</td> <td><xsl:value-of select="book/category" /></td> </tr> <tr> <td>author</td> <td><xsl:value-of select="book/author" /></td> </tr> <tr> <td>description</td> <td><xsl:value-of select="book/description" /></td> </tr> </table> </body> </html> </xsl:template> </xsl:stylesheet> Copy after login |
然后我们在book.xml文件中加入对这个XSLT文件的引用。
book.xml
12345678 Copy after login | <?xml version="1.0" encoding="ISO-8859-1"?> <?xml-stylesheet type="text/xsl" href="book.xsl"?> <book> <name>Effective JavaScript</name> <category>Program Language</category> <author>Bowen</author> <description>This book is about JavaScript Language.</description> </book> Copy after login |
接下来我们再用浏览器打开book.xml文件,发现显示变成了这样。是不是很神奇?
注意如果你使用chrome打开该book.xml文件,请设置chrome的--allow-file-access-from-files属性,这样chrome才允许加载本地的xsl文件。解决方案看这里:http://stackoverflow.com/questions/3828898/can-chrome-be-made-to-perform-an-xsl-transform-on-a-local-file
OK,这篇文章讲的够多了,下篇接着讲XPath,XML to Object以及XML文档格式与近来风头强劲的JSON、YAML格式的比较。