Practical Tips for XML volumes (2): dynamic query

Source: Internet
Author: User

Motivation:
The query function is the most common and commonly used function module we have seen on the website. In the past, all information queries were connected to the database, and each click must be supported by the background database. However, in many cases, users only operate on a certain part of the data, which not only increases the server load, but also seriously affects the user's browsing speed.

In this case, we need to pass a part of the data that the user needs to the client in XML format, which can be easily operated by the user. It not only facilitates users, but also reduces the burden on the server database. Why not! This function can be used in many other modules. Therefore, this dynamic query function is added.

MATERIALS:
Dynamic query of XML volumes
There are two files: search. xml and search. XSL.

Purpose:
Filter and filter data without refreshing the page, which effectively improves the data query function.
Effect:
Browse here
Code :
Search. xml
<? XML version = "1.0" encoding = "gb2312"?>
<? XML-stylesheet type = "text/XSL" href = "search. XSL"?>
<Blueidea>
<Team>
<Blue_id> 1 </blue_id>
<Blue_name> sailflying </blue_name>
<Blue_text> A simple query </blue_text>
<Blue_time> 17:35:33 </blue_time>
<Blue_class> XML topic </blue_class>
</Team>
<Team>
<Blue_id> 2 </blue_id>
<Blue_name> flyingbird </blue_name>
<Blue_text> marry you. It hurts you. </blue_text>
<Blue_time> 12:45:51 </blue_time>
<Blue_class> irrigation essence </blue_class>
</Team>
<Team>
<Blue_id> 3 </blue_id>
<Blue_name> sub </blue_name>
<Blue_text> application of regular expressions in UBB Forum </blue_text>
<Blue_time> 21:02:16 </blue_time>
<Blue_class> Web programming essence </blue_class>
</Team>
<Team>
<Blue_id> 4 </blue_id>
<Blue_name> taiyilang </blue_name>
<Blue_text> end-of-year full manual v0.1 for classic rudder gathering </blue_text>
<Blue_time> 10:22:48 </blue_time>
<Blue_class> Forum Irrigation Area </blue_class>
</Team>
<Team>
<Blue_id> 5 </blue_id>
<Blue_name> mmkk </blue_name>
<Blue_text> Asp error message summary </blue_text>
<Blue_time> 16:39:05 </blue_time>
<Blue_class> Javascript script </blue_class>
</Team>
</Blueidea>

Search. XSL
<? XML version = "1.0" encoding = "gb2312"?>
<XSL: stylesheet xmlns: XSL = "http://www.w3.org/TR/WD-xsl">
<XSL: template match = "/">
<HTML>
<Head>
<Title> practical tips for XML volumes (2): dynamic query </title>
<Style>
Body, blueidea, team, blue_id, blue_name, blue_text, blue_time, blue_class {Font: 12px "", "Arial", "Times New Roman ";}
Table {font-size: 12px; Border: 0px double; border-color: #99cc99 #99cc99 # cccccc # cccccccc; cellpadding: 3; cellspacing: 3; bgcolor: # eeeeee; text-Decoration: Blink}
SPAN {font-size: 12px; color: red ;}
</Style>
<SCRIPT>
Function searchtext (X)
{
Stylesheet = Document. Document document;
Source = Document. xmldocument;
Sortfield = Document. invalid document. selectnodes ("// @ select ");
If (X! = "")
{
Sortfield [1]. value = "team [blue_id = '" + x + "']";
Layer1.innerhtml#source.doc umentelement. transformnode (stylesheet );
}
Else {alert ("Please enter filtering conditions! ");}
}
</SCRIPT>
</Head>
<Body>
<P align = "center"> <span> practical tips for XML volumes (2): dynamic query </span> </P>
<Div id = "layer1" name = "layer1">
<XSL: Apply-templates select = "blueidea"/>
</Div>
<HR size = "1" width = "500"/>
<Table align = "center" cellpadding = "0" cellspacing = "0" border = "0">
<Tr>
<TD>
<Span> enter filtering conditions: </span>
Blue_id = <input type = "text" name = "searchtext" size = "1" maxlength = "1"/>
<Input type = "button" class = "button" onclick = "searchtext (document. All. searchtext. Value)" value = "Search" name = "button"/>
</TD>
</Tr>
</Table>
</Body>
</Html>
</XSL: Template>
<XSL: template match = "blueidea">
<Table width = "500" border = "1" align = "center" cellpadding = "1" cellspacing = "1" bordercolordark = "# ffffff" bordercolorlight = "# adaaad">
<Tr bgcolor = "# ffcc99" align = "center">
<TD> NO. </TD>
<TD> name </TD>
<TD> topic </TD>
<TD> posting time </TD>
<TD> Category </TD>
</Tr>
<XSL: Apply-templates select = "team" Order-by = "blue_id"/>
</Table>
</XSL: Template>
<XSL: template match = "team">
<Tr align = "center">
<XSL: Apply-templates select = "blue_id"/>
<XSL: Apply-templates select = "blue_name"/>
<XSL: Apply-templates select = "blue_text"/>
<XSL: Apply-templates select = "blue_time"/>
<XSL: Apply-templates select = "blue_class"/>
</Tr>
</XSL: Template>
<XSL: template match = "blue_id">
<TD bgcolor = "# eeeeee">
<XSL: value-of/>
</TD>
</XSL: Template>
<XSL: template match = "blue_name">
<TD>
<XSL: value-of/>
</TD>
</XSL: Template>
<XSL: template match = "blue_text">
<TD>
<XSL: value-of/>
</TD>
</XSL: Template>
<XSL: template match = "blue_time">
<TD>
<XSL: value-of/>
</TD>
</XSL: Template>
<XSL: template match = "blue_class">
<TD>
<XSL: value-of/>
</TD>
</XSL: Template>
</XSL: stylesheet>

Explanation:
1) Search. XML is a data file, and we believe there will be no problem.
2) search. XSL is a format file. There are several points to note.
(1) In the script:
Sortfield = Document. invalid document. selectnodes ("// @ select ");
The role is to find all nodes with the select attribute. This is what I mentioned in dynamic sorting.
Sortfield = Document. Document. selectsinglenode ("// @ order-");
There are some differences. Pay attention to this small difference and their respective functions.

Sortfield [1]. value = "team [blue_id = '" + x + "']";
Therefore, sortfield [1] is the second node, and its corresponding node is
<XSL: Apply-templates select = "team" Order-by = "blue_id"/>

Parameter X is the value entered in the text box.
Modify the Search Condition of select = "team" to select = "team [blue_id = 'X']"
The function is to add judgment conditions. Only XML data with the value of blue_id equal to X is displayed.
Of course, you can enrich the judgment conditions. The simple judgment I made here is to make it easier for everyone to understand.
Finally, the new sorting content is displayed by re-displaying the innerhtml value of layer1.

(2) text:

Select = "team"
Here it is sortfield [1], but it may be changed when you do it.
So you must calculate the accuracy!
I provide a common method: in the code, you can use loops to determine whether the node is required for you.

Another point:
XML has extremely strict requirements on Case sensitivity. If your writing is not standard, it will catch a cold!

Postscript:
After you are familiar with the dynamic sorting and dynamic query completion ideas, you will find that our implementation method is very simple.
Is to modify a value, and then re-display.
We still follow this idea in the dynamic paging function.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.