PHP + XML + XSLT beginner notes

xiaoxiao2021-03-06  14

Recently study the use of PHP XML XSLT, check some information, close to note!

Configuration

Copy the following files under the PHP / DLLS directory to Windows / System32 expat.dll sablot.dll 2. Modify php.ini

Modify EXTENSION_DIR to point to the correct extension directory extension_dir = c: / php / extensions and find the following line; extension = php_sablot.dll modified to: extension = php_sablot.dll

In fact, loading a PHP_SABLOT module

3. The test started

This is a PHP file, he can directly convert things in the database into XML output, temporarily name Test.php

run_sql_return_xml ("Select * from assetro");

Class Cmysqlxml {

Var $ host;

Var $ user;

Var $ Password;

VAR $ db;

Function CMYSQLXML ($ Host, $ User, $ Password, $ db) {

$ this-> Host = $ host;

$ this-> user = $ us;

$ this-> Password = $ password;

$ this-> db = $ db;

}

Function Run_SQL_RETURN_XML ($ SQL_STRING) {

$ connection = mysql_connect ($ this-> Host, $ this-> User, $ this-> password, $ this-> dB);

MySQL_SELECT_DB ($ this-> dB);

$ result = mysql_query ($ sql_string); // Results set

$ DOC = Domxml_Open_Mem (""); // Create a DOM object

While ($ row = mysql_fetch_array ($ results, mysql_assoc) {

$ num_fields = mysql_num_fields ($ result); // Number of columns

$ row_element = $ doc-> CREATE_ELEMENT (mysql_field_table ($ result, 0)); // Creating a table name

$ doc_root = $ doc-> document_element (); // Determine root as document element

$ ROW_ELEMENT = $ doc_root-> append_child ($ row_element); // Add column name element to root document for ($ I = 0; $ i <$ Num_fields; $ FIELD_NAME = mysql_field_name ($ Result, $ I) ; // take the field name $ col_element = $ doc-> create_element ($ field_name); // create elements $ col_element = $ row_element-> append_child ($ col_element); // add elements $ text_node = $ doc-> create_text_node (iconv ("GB2312", "UTF-8", $ ROW [$ FIELD_NAME]))); // To show convenient special ICONV to transfer $ col_Element-> append_child ($ text_node); // Add value

}

}

MySQL_Free_Result ($ Result);

MySQL_Close ($ connection);

// Return HTMLSpecialchars ($ doc-> dump_mem (false)); Return $ DOC-> Dump_Mem (false);}

}

Here's this is an XSL file, and the function is to output two fields in XML data.

Here I compare XSLT and CSS, which is conducive to everyone understands XSLT

CSS can also format XML documents, then why do CSS need XSLT? Because CSS is capable of controlling the style, such as color, font, size, but it has a serious limitations, that is:

(1) CSS cannot reorder the elements in the document;

(2) CSS cannot determine and control which element is displayed, which is not displayed;

(3) CSS cannot statistically calculate the data in the element;

In other words, CSS is only suitable for outputting a fixed final document. The advantage of CSS is simple, consumes less system resources; while XSLT is powerful, but because the XML tree is required, it is more memory.

Therefore, we often combine them, such as using the XSLT processing document in the server, controlling the CSS. Can reduce the response time.

The two fields in the example are astroid start_date, which is the name of the field in the field, according to the needs of it.

Title

Artist

Finally, the XML and XSL are integrated, converted into HTML output, and it is a PHP file

// store xmlstring in an array using a key '/ _xml' $ arguments = array ('/ _ xml' => $ xmlstring);

Echo (xslt_process ($ xh, 'arg: / _ xml', $ xslfile, null, $ arguments));

XSLT_FREE ($ xh);?>

The program of a simple PHP XML XSL is generated. Is a data form

If you get $ xslfile = "htdocs / XML / Test.xsl"; change to file_get_contents, the program becomes the following way

// store xmlstring in an array using a key '/ _xml' $ arguments = array ('/ _ xml' => $ xmlstring, '/ _xsl' => $ xslfile;

Echo (xslt_process ($ xh, 'arg: / _ xml', 'arg: / _ xsl', null, $ arguments);

XSLT_FREE ($ xh);?>

The result of running is true, first write here, pick it up, ready to learn about XSLT, there are many things, some research.

转载请注明原文地址:https://www.9cbs.com/read-49375.html

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.035, SQL: 9