CSS (laminated style sheet) and XSL (extensible style language) can define display of XML files, which differ in both ways and their specific methods in use, we will introduce this article.
In the XML file, it is basically a customized tag, apparently a browser is unable to understand these tags, now, the browser is just a parser as an XML file ?? As long as your XML file is Well-Formed , Then it will show you the files. The content and expression form in XML are separated, and there is no information in the form of expression in an XML source file. The biggest feature of XML is to reveal the meaning of the information itself, and the electronic document exchange for automation is optimal. If an XML file is only used to exchange information, there is no need to consider its display problem. Edit the XML file, we only need to pay attention to the contents of the file, the structure of the information, as for how it is displayed, then handed over to the CSS (laminated style sheet) and XSL (Extensible style language) to complete. This allows the user to define the form of expression of the data as needed.
■ Use CSS to express XML
CSS has a good role in HTML, in XML, CSS also plays its powerful style sheet. CSS and HTML in XML are similar. The current version is CSS 2.0.
Let's first look at the first.css of the CSS code. It is a CSS file made in Example 2.XML this XML file.
In the source file of the 2.xml this XML file, there is a line: Xml-stylesheet href = "first.css" type = "text / css"?>, It specifically references the CSS file when the XML file is displayed, The specific syntax is as follows: Xml-stylesheet href = "url" type = "text / css"?> Where XML-Stylesheet is a keyword, indicating reference style sheet settings; href = "url" is used to specify a style sheet The location, in Example 1, because first.css and 2.xml in the same directory, it can be written directly, it is the relative address; type = "text / css" indicates that the style sheet is CSS Style sheet. Before applying CSS, the browser displays as shown in Figure 1. After the style sheet is applied, the display effect is shown in Figure 2.
Below we come to analyze the file of first.css in Example 1. As you can see, the CSS syntax in the XML and the CSS syntax in HTML is actually the same. It is all how to set the tag text by some tags.
Controls Table 1 and Table 2 to analyze this file. In this file, each block is set for different tags in the XML file, and each block is separated by {} symbol. Like the first block, set the color (color) of the Title tag, display status (whether segmentation: block or inline; hidden: none), Font-Family, Size (FONT- Size and font-weigh, there is no upper / underline (Text-Decoration). Other blocks, you can analyze some common attributes in the table. We can use the various attributes of CSS to combine colorful display effects. This is not big in programming, because the syntax is too simple, the key to see your imagination.
For the same XML file, if we give it different CSS, then there is a different display effect. If your web is made with XML, then in order to transform the web page, you can edit the plurality of CSS files, the segmentation time replace. It is only necessary to change the "URL" specified in the XML file to change the CSS file. ■ Use XSL to express XML
XSL (Extensible Styles Language) is also a specification that displays an XML file. Unlike CSS: XSL is formulated in accordance with XML specification. That is, the XSL file itself meets XML's grammatical provisions. XSL is powerful than CSS in the typographic style. For example: CSS is suitable for files that are constant in elements, it cannot change the order of elements in the XML file. The elements are arranged in the XML file, then the order in which CSS is expressed. We must use XSL for files that need to be sorted by different elements.
How is XSL work? The XML file is a tree structure after the expansion is called "original tree", the XSL processor (now only IE 5 supports XSL, the processor called: XSL Stylesheet Processor) from this tree structure Information, sort this "original tree" according to the instructions of the XSL style, copy, filter, filter, delete, select, another "result tree" after the operation, etc., then add some new display control information in the "Result Tree". For forms, other texts, graphics, and some information about display formats. The XSL processor reads the information in the XML file based on the instruction of the XSL style sheet, and then recombines the HTML file to generate a Well-Formed HTML file. The browser shows that the HTML file is definitely no problem, so that the information in the XML file is displayed in front of us in a certain form.
Similarly, let's first look at an example of XSL, then analyze its structure and syntax. XML source files and XSL source files As shown in Example 2, the display effect is shown in Figure 3. It can be seen that if you want the XML file to use XSL to perform, you should add a sentence in front of the XML file: Xml-stylesheet href = "URL" type = "text / xsl"?>, And reference CSS is the same, only But it is specified Type = "text / xsl".
Take a look at the XSL file, you can find that the structure of the XSL file and the structure of the XML file are the same, because XSL rules are based on XML rules. A XSL file must also be Well-FoeMed because the tags used in the XSL file are defined by W3C. So, the XSL file does not require DTD. Alternatively, you can see that some HTML tags can be inserted in the XSL file to help the XML file display, these HTML tags must also be Well-Formed, for example, must have the correct end tag (not like files in HTML file) It can be lazy as it is.
The XSL file itself is an XML file, so the same statement is the same as the XML file at the beginning of the XSL file. W3C defines a lot of tags (elements) for XSL, and the XSL file is a combination of these tags and HTML tags. In the XSL file, you must have:
Among them, XSL: Stylesheet is the root element of the XSL file, which contains all typesetting styles in the root element. The style sheet is combined by these typesetting styles; XMLns: XSL = "http://www.w3.org/tr / WD-XSL "This sentence is mainly used to indicate that the XSL style sheet is the XSL developed using W3C, and the set value is the URL address where the XSL specification is located. In the XSL file, in addition to the HTML tag, others are marked by XSL. Marks have different functions. The following describes a variety of available tags in XSL, you can refer to the functional description and use examples of the example 3, to see the source files of XSL in Example 3.
1. XSL: Stylesheet: As the root element in the XSL style sheet, there must be in each XSL file.
Property: Default-space: Decide if the blank in the XML file is retained, only when the value is "default".
Indent-result: Decide whether to keep blank in the XSL file, the value is reserved when "Yes".
Language: Set the scripting language used in the XSL file.
Example:
2. XSL: Template: Specify specific tags in the XML file to define the typographic style.
Property: Language: Which scripting language is specified.
Match: Set which tag from the XML file begins to read the information, if the value is "/", then the root element from the XML file begins to read information.
Example:
3. XSL: value-of: reads the information from the specific tag in the XML file. Property: SELECT: Sets the information in which one tag is read.
Example:
image 3
4. XSL: For-Each: Apply the typographic style to the same tag in the XML file (similar to a loop statement).
Property: SELECT: Sets it to read data from which tag.
ORDER-BY: After reading the information is completed, the setting is sorted according to what tag, the value is a particular tag, such as using the " " number before the tagnap, is sorted by large to small, "-" number. Example:
5. XSL: Comment: In this element, XSL uses it as an comment message and is not displayed in the browser.
6. XSL: Apply-Templates: Indicates the XSL processor to find the style set by the appropriate
Properties: ORDER-BY and SELECT: like XSL: for-EACH.
example:.......
........
.........
7. XSL: Copy: Information from the XML file in the information in the output to the output file. No properties.
example:.......
XSL: COPY>
Xsl: template>
...........
This example first reads all the non-marked information in the XML file all in
8. XSL: IF: Similar to the IF ... THEN in the general program.
Property: Script: Sets the formula expressed by the Script program.
Language: Set which scripting language to use.
Test: Descript of setting conditions. Only when the setting value of the Script property is transmitted back to "TRUE" (or the condition of TEST property settings), the content in
Example:
Xsl: if>
9. XSL: Choose, XSL: WHEN, XSL: OtherWise: These three elements are used to set more complicated conditional formulas, which generally cooperate together. The xsl: when there is three properties of Script, Language, Test, which is the same as the attribute meaning in the previous XSL: IF.
Example:
Xsl: when>
Xsl: when>
Xsl: OtherWise>
Xsl: choose>
10.xsl: Attribute and XSL: ELEMENT: You can add a property name or create a new tag in the tag. The XSL style sheet can not only reference HTML tags, but also create new tags and properties, and then display the information in the XML file together. Where XSL: Attribute is a new attribute in the tag, XSL: Element is a new tag. They have a common attribute.
Property: Name: Specifies the name of the property for the newly built attribute.
Example: If we don't have the HTML tag of the graphic file, we want to add such tags in the XSL file, named , with SRC properties.
Xsl: attribute>
Xsl: element>
XSL is the best way to display XML files because it meets the standard of XML files, and all XML has the advantages, XSL has, and there is also a more CSS than CSS, so we want to put XML file content When it is displayed, it is best to give priority to the use of XSL ?? for future expansion.
Example 2 2.XML Source File
XML Version = "1.0" encoding = "gb2312"?> Xml-stylesheet href = "first.xsl" type = "text / xsl"?>
Book>
Book>
Data>
First.xsl source file
XML Version = "1.0" encoding = "GB2312"?>