Home >Backend Development >XML/RSS Tutorial >Detailed introduction to graphic code of XML-based desktop applications
As mentioned earlier, through XSL, we can submit the same data to end customers in different data forms. An XSL file describes the display method of the data. You can connect many XSLs to the same xml document to provide Different HTML-based representations, so in fact, we can build XML-based desktop applications. A graphical representation based on the Windows architecture looks like this:
There are two main advantages to using this approach. First, you can operate in a platform- and language-independent manner. Data, secondly, you can implement different view representations of the same data without programming.
Actually, you need a tool that can convert the original data into XML format, and then express it in HTML format. In fact, in IE5.0, you can achieve different views of data through its support for XSL. You can use the built-in XSL processor to produce output from an XML stream. Developers can write different XSL scripts and then process them using IE's built-in XSL processor. On the other hand, we can convert the recordset into XML format through the inherent functions of the ADO component. The sample code is as follows:
If Dir(XML_FILE) = "" Then rs.Save XML_FILE, adPersistXML End If 这里常量adPersistXML表示使用一个XML纲要来保存记录集的内容 我们再仔细看一下Recordset对象的Save方法,其定义如下: Save([FileName As String], [PersistFormat As PersistFormatEnum = adPersistADTG])
Each parameter is optional, but when you first convert the recordset When saving to disk, you must specify a file name. If there is a valid filter when this method is executed, only data that passes the filter can be saved. When the Recordset object's method Close is called to close the Recordset object, the file is automatically closed. After the Recordset is saved to the disk, you can use the Open method to read it. The specific code is as follows:
rs.open "c:\myrs.xml"
Then we analyze the issue about the display of XML data. IE5's support for XSL is based on the recent W3C draft standards established. Here we discuss using VB to build an application that accesses data in the data source through the ADO interface. The resulting record set can be displayed in different formats. By writing XSL documents, you can add different views at will.
For example, once you extract a result set, you can use the DataGrid control in VB to conveniently display the data. The specific code is as follows:
Set rs = New ADODB.Recordset rs.CursorLocation = adUseClient queryString = "select * from Employees" rs.Open queryString, "NorthWind" 'NorthWind是微软的示例数据库,很容易在 'access或者是SQL SERVER中找到 rs.ActiveConnection = Nothing Set DataGrid1.DataSource = rs
In many cases, a view of this is not enough, you need strict control over each field. The records in the entire table may be represented in the form of both tables and trees. Providing multiple views will make users feel more comfortable. The combination of XML and XSL is a good solution to this problem. You can make full use of the functionality of the WebBrowser control in IE5 for direct browsing.
The ADO component itself does not generate any XSL documents from the XML documents it generates. This doesn't mean that you can't write a generic XSL document yourself to change the way the data is displayed. For example, in order to automatically display an XML document containing an ADO record set in table form, you need to add the following code to the head of the XML document:
<?xml-stylesheet type="text/xsl" href="simple.xsl"?>
This simple.xsl file must be placed with the XML document In the same directory, it contains two nested loops. The first loop enumerates all attributes of the element. The names of these attributes are displayed as titles on the first line. The second loop is used to output all records in the recordset. .
The code of the simple.xsl file is as follows:
<?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl" xmlns:html="http://www.w3.org/TR/REC-html40" result-ns="" language="JScript"> <xsl:template match="/"> <html> <head> <title>RowsetSchema</title> <style> .stdText { font-Family:verdana; font-Size: 9px; } </style> </head> <body> <table width="100%" border="1"> <xsl:for-each select="xml/s:Schema/s:ElementType/s:attribute"> <th class="stdText"><xsl:value-of select="@type" /></th> </xsl:for-each> <xsl:for-each select="xml/rs:data/z:row"> <tr> <xsl:for-each select="@*"> <td class="stdText" valign="top"><xsl:value-of match="@*"/></td> </xsl:for-each> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>
What needs to be noted here is the syntax for listing all attributes of a given node. In fact, we need to get any
By using the WebBrowser control as the engine for browsing your data, you can use XML and XSL to separate the content from the presentation layer. In this model, XSL acts as a markup. language (rather than a programming language) to describe how to display XML data. In order to add a new view, you only need to add a corresponding XSL document. In fact, this Web-based document/view model can be combined with the document under MFC. / View model analogy
Finally, we need to talk about the difference between XSL and CSS? The key difference is that XSL is applied to the entire XML document, while CSS only generates the display form of HTML tags. Impact. XSL is a markup language for processing documents, and CSS is a set of attributes used to represent the attributes of an element in the markup language. Moreover, XSL can use CSS to generate HTML output from the XML data stream. From a perspective, they are two different technologies.
The above is the detailed introduction of the graphic code of XML-based desktop applications. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!