XML file display - CSS and XSL

zhaozj2021-02-16  58

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 custom 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 see a CSS code - First.css in Example 1. 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: , It specifically references the CSS file when the XML file is displayed, The specific syntax is as follows: 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.

Figure 1 Let's analyze the FIRST.CSS file 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 combine colorful display effects in the various properties of the N-Deman Dizui SS. 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 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 cannot be changed. 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 take a 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: , And reference CSS is the same, only But it is specified Type = "text / xsl".

Editor's Recommended Read ● Topic: WML Language Plenary ● XML and its related technique to see the XSL file, you can find the structure of the XSL file and the structure of the XML file, because XSL rules are based on the rules of XML. 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: : Indicates that read information from the tag in the XML file.

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: : Indicates that the information is read from the tag.</p> <p>image 3</p> <p>4. XSL: For-Each: Apply the typographic style to the same tag in the XML file (similar to a loop statement).</p> <p>Property: SELECT: Sets it to read data from which tag.</p> <p>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: <xsl: for-each select = "data / book" ORDER-BY = "- price">: Because there are multiple <book> tags in the XML file, here is set repeatedly from <book> marked Read information in child elements, and sort the read information according to the price from a small to large.</p> <p>5. XSL: Comment: In this element, XSL uses it as an comment message and is not displayed in the browser.</p> <p>6. XSL: Apply-Templates: Indicates the XSL processor to find the style set by the appropriate <XSL: Template> in the XSL style sheet.</p> <p>Properties: ORDER-BY and SELECT: like XSL: for-EACH.</p> <p>example:.......</p> <p><Tr> <xsl: Apply-Templates /> </ Tr> ......... <xsl: template match = "book"> <td> <xsl: value-of select = "author" / > </ Td> </ xsl: template> ......... 7. XSL: Copy: Information from the XML file in the information in the output to the output file. No properties.</p> <p>example:.......</p> <p><XSL: Template> <XSL: COPY> <XSL: Value-of /> </ xsl: Copy> </ xsl: template> .........</p> <p>This example first reads all the non-marked information in the XML file all in <XSL: COPY>, then displayed by <XSL: Value-of /> to display the information of these copies.</p> <p>8. XSL: IF: Similar to the IF ... THEN in the general program.</p> <p>Property: Script: Sets the formula expressed by the Script program.</p> <p>Language: Set which scripting language to use.</p> <p>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 <XSL: if> is processed by the XSL processor.</p> <p>example:</p> <p><Xsl: if test = ". [@ Sex = 'male']> <td> Male <xsl: value-of /> </ td> </ xsl: if></p> <p>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.</p> <p>example:</p> <p><Xsl: choose> <xsl: when test = "wife"> <td> Wife <xsl: value-of /> </ td> </ xsl: by> <xsl :hen test = "husband"> <td> Fu <xsl: value-of /> </ td> </ xsl: by> <xsl: OtherWise> <TD> Unmarried </ TD> </ XSL: OtherWise> </ xsl: choose></p> <p>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.</p> <p>Property: Name: Specifies the name of the property for the newly built attribute.</p> <p>Example: If we don't have the HTML tag of the graphic file, we want to add such tags in the XSL file, named <img>, with SRC properties.</p> <p><Xsl: template match = "image"> <xsl: element name = "img"> <xsl: attribute name = "src"> <xsl: value-of /> </ xsl: attribute> </ xsl: Element></p> <p>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.</p> <p>Example 2 2.XML Source File</p> <p><? XML Version = "1.0" encoding = "gb2312"?> <? Xml-stylesheet href = "first.xsl" type = "text / xsl"?> <Data> <book> <title> XML Getting Started / title> <author> Zhang 3 </ author> <price unit = "RMB"> 20.00 </ price> </ book> <book> <title> xml syntax </ title> <! - will be published-> <Author> Li 4 </ author> <price unit = "RMB"> 18.00 </ price> <memo> This book is a famous computer scientific expert, which is very authoritative. In the book, we will insert a lot of exciting examples, so you can readability. </ Memo> </ book> </ data> first.xsl source file <? Xml version = "1.0" encoding = "gb2312"?> <Xsl: stylesheet xmlns: xsl = "http://www.w3.org / TR / WD-XSL "> <XSL: Template Match =" / "> <html> <body> <center> <h2> Book information </ h2> </ center> <div align =" center "> <center > <Table border = "1" cellpadding = "5" bgcolor = "# 4eb7de"> <tr> <TH> <TH> <TH> </ tH> <TH> price (RMB) </ TH > <TH> Note </ TH> </ Tr> <xsl: for-each select = "data / book" ORDER-by = "title"> <tr> <td> <xsl: value-of select = "Title "/> </ Td> <TD> <xsl: value-of select =" author "/> </ td> <td> <xsl: value-of select =" price "/> </ td> <td width = "150"> <xsl: value-of select = "memo" /> </ td> </ tr> </ xsl: for-each> </ table> </ center> </ div> </ body> </ Html> </ xsl: Template> </ xsl: styleSheet> Table 1 Common text-related properties</p> <p>Property Name Function Description Settings Value COLOR Word Color Color English English Name or Hexagon RGB Color: Red Color: # f00f01 Font-Family Text Type Font Name Font-Family: Song Font-Size Word Size PT , CM, PX, XX-Small, XX, Large, X-Large, XX-Large Font-size: X-Large Font-Size: 1cm font-weight text thick EXTRA-LIGHT, LIGHT , Demi-Light, MEDIUM, DEMI-BOLD, BOLD, EXTRA-BOLD FONT-Weight: Light Font-Style Fonts Style Normal, Italic Font-Style: Italic Text-Align Text Position Center, Right, Left Text-Align: Left TEXT-INDENT text constitutional PT (point) in (inch) cm (cm) PX (pixel) Text-Indenter: 20pt text-transform: Uppercase text-transform: Uppercase text-decoration text wiring settings Underline, Ouerline, Line-Through TEXT-DECORA: Underline Table 2 Common layout properties</p> <p>Property Name Function Description Settings Values ​​None, Block, Inline Width Components Width PT, BLOCK, and PX Background-Color Components Background Color Color English Name or Hexagon RGB Padding Components Distance PT, In, CM, PX Padding-Left components and left Distance PT, In, CM, PX Padding-Right components and the right distance PT, In, CM, PX Padding-TOP components and the distances of the upper side PT, in, CM, PX Padding-Bottom Components with the lower side PT, In, CM, PX Border Square Border Border-Left Square left box Border-Right Square left box Border-Top Square upper box Border-bottom square down Box Border -Color Square Border Color Color English Name or Hexagon RGB Border-Wide Square Border Width Thin, Medium, Thick, PT, In, CM, PX Border-Style Standard Style None, Dotted, Solid, Double</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-22374.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="22374" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.036</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'Pcs94hFzXUp4eEO1oZIYHMZTC6MBnzDEcS89vN3EVSpWKrfWsKiRPsZYwTDmk3af4oR3CcZE2WRoNVH_2B'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>