The actual combat of XML volume (2): Dynamic query

Source: Internet
Author: User
Tags filter modify numeric value query regular expression sort version xsl
Xml| News

Motivation:
Query function is the most common and most commonly used function module that we have seen on the website. Previous information queries are connected to the database, each click must be back to the background database support. However, in many cases, users often only for a certain part of the data to operate, so that not only the burden of the server is heavier, but also seriously affect the speed of user browsing.

In this case, we need to pass a certain part of the data that the user needs to the client in XML, which can be easily manipulated by the user. It is convenient for users, but also reduces the burden of server database. Why not! And this feature can be generalized to many other modules, so add this dynamic query function.

Materials:
Dynamic query of XML volume
There are 2 files: Search.xml and search.xsl

Role:
Filter and filter the data without refreshing the page, effectively improve the function of the data query.
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>2002-1-11 17:35:33</blue_time>
<blue_class>xml Special Topics </blue_class>
</team>
<team>
<blue_ID>2</blue_ID>
<blue_name>flyingbird</blue_name>
<blue_text> marry you, is to you pain </blue_text>
<blue_time>2001-09-06 12:45:51</blue_time>
<blue_class> Irrigation Essence </blue_class>
</team>
<team>
<blue_ID>3</blue_ID>
<blue_name> The harsh child </blue_name>
The application of <blue_text> regular expression in UBB forum </blue_text>
<blue_time>2001-11-23 21:02:16</blue_time>
<blue_class>web Programming Essence </blue_class>
</team>
<team>
<blue_ID>4</blue_ID>
<blue_name> b Lang </blue_name>
<blue_text> end of the year Classic Rudder party complete manual v0.1</blue_text>
<blue_time>2000-12-08 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 Information </blue_text>
&LT;BLUE_TIME&GT;2001-10-13 16:39:05</blue_time>
<blue_class>javascript Scripts </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= "/" >
<title> XML volume of actual combat (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 #CCCCCC, Cellpadding:3;cellspacing:3 b Gcolor: #eeeeee; Text-decoration:blink}
span {font-size:12px; color:red;}
</style>
<script>
function SearchText (x)
{
Stylesheet=document. Xsldocument;
Source=document. XmlDocument;
Sortfield=document. Xsldocument.selectnodes ("//@select");
if (x!= "")
{
Sortfield[1].value= "Team[blue_id= '" +x+ "" ";
Layer1.innerhtml=source.documentelement.transformnode (stylesheet);
}
else {alert ("Please enter a filter condition!") ");}
}
</script>
<body>
<p align= "Center" ><span>xml volume of the actual combat Kam (2): Dynamic Query </span></p>
<div id= "Layer1" name= "Layer1" >
<xsl:apply-templates select= "Blueidea"/>
</div>
&LT;HR size= "1" width= "/>"
<table align= "center" cellpadding= "0" cellspacing= "0" border= "0" >
<tr>
<td>
<span > Please enter filter criteria: </span>
blue_id= <input type= "text" name= "SearchText" size= "1" maxlength= "1"/>
<input type= "button" class= "button" value= "Search" name= "button"/>
</td>
</tr>
</table>
</body>
</xsl:template>
<xsl:template match= "Blueidea" >
<table width= border= "1" align= "center" cellpadding= "1" cellspacing= "1" bordercolordark= "#ffffff" bordercolorlight= "#ADAAAD" >
<tr bgcolor= "#FFCC99" align= "Center" >
<td> number </td>
<td> name </td>
<td> Topics </td>
<td> Publishing Time </td>
<td> Classification </td>
</tr>
<xsl:apply-templates select= "Team" order-by= "blue_id"/>
</table>
</xsl:template>
<xsl:template match= "Team" >
&LT;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" >
&LT;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>


Explain:
1 Search.xml is a data file, I believe we will not have any problems.
2 search.xsl is a format file, there are several places to pay attention to.
(1) in the script:
Sortfield=document. Xsldocument.selectnodes ("//@select");
The function is to find all nodes that have properties of select. And that's what I'm talking about in the dynamic sort.
Sortfield=document. Xsldocument.selectsinglenode ("//@order-by");
A little different. Attention to this small difference as well as their respective functions.


Sortfield[1].value= "Team[blue_id= '" +x+ "" ";
So Sortfield[1] is the second node found, and its corresponding node is
<xsl:apply-templates select= "Team" order-by= "blue_id"/>

The parameter x is the numeric value entered in the text box.
We modify the search criteria for the select= "team" to select= "team[blue_id= ' x '".
The effect is: to increase the judgment condition, only the blue_id value equals x's XML data to display.
Of course, we can enrich the conditions of judgment, I am here to make a simple judgment is to make it easier to understand.
Finally, the new sort content is displayed by Layer1 the innerHTML value of the display.

(2) in the text:

select= "Team"
In my case it is sortfield[1], but you may change it when you do it.
Then you have to calculate accurate but not wrong oh, or find another to go!
I offer a common approach: in code you can use loops to determine whether you need a node.


Another point:
XML is extremely demanding in case of capitalization. So your writing is not standard, it will catch a cold!


Postscript:
We are familiar with dynamic sorting and dynamic query after the completion of ideas will be found, in fact, our implementation of the method is very simple.
is to modify a value and then display it again.
In the dynamic paging function we are still in accordance with this idea to complete.



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.