Home >Backend Development >XML/RSS Tutorial >XML Practical Cheats Volume 2: Dynamic Query

XML Practical Cheats Volume 2: Dynamic Query

巴扎黑
巴扎黑Original
2017-03-19 15:43:181512browse

[Introduction] The query function is the most common and commonly used functional module we have seen on the website. In the past, information queries were connected to the database, and every click required the support of the backend database. However, in many cases, users often only operate on a certain part of the data. This not only serves the query function, it is the most common and commonly used functional module we have seen on the website. In the past, information queries were connected to the database, and every click required the support of the backend database. However, in many cases, users often only operate on a certain part of the data, which not only increases the burden on the server, but also seriously affects the user's browsing speed.

In this case, we need to pass a certain part of the data that the user needs to the client in the form of xml, so that the user can easily operate on this data. It not only facilitates users, but also reduces the burden on the server database. Why not do it! And this function can be applied to many other modules, so this dynamic query function has been added.

Materials:
XML Volume Dynamic Query
There are 2 files: search.xml and search.xsl

Function:
Query without refreshing the page The data is filtered and filtered to effectively improve 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>一个简单的查询</blue_text>
    <blue_time>2002-1-11 17:35:33</blue_time>
    <blue_class>XML专题</blue_class>
  </team>
  <team>
    <blue_ID>2</blue_ID>
    <blue_name>flyingbird</blue_name>
    <blue_text>嫁给你,是要你疼的</blue_text>
    <blue_time>2001-09-06 12:45:51</blue_time>
    <blue_class>灌水精华</blue_class>
  </team>
  <team>
    <blue_ID>3</blue_ID>
    <blue_name>苛子</blue_name>
    <blue_text>正则表达式在UBB论坛中的应用</blue_text>
    <blue_time>2001-11-23 21:02:16</blue_time>
    <blue_class>Web 编程精华</blue_class>
  </team>
  <team>
    <blue_ID>4</blue_ID>
    <blue_name>太乙郎</blue_name>
    <blue_text>年末经典分舵聚会完全手册 v0.1</blue_text>
    <blue_time>2000-12-08 10:22:48</blue_time>
    <blue_class>论坛灌水区</blue_class>
  </team>
  <team>
    <blue_ID>5</blue_ID>
    <blue_name>mmkk</blue_name>
    <blue_text>asp错误信息总汇</blue_text>
    <blue_time>2001-10-13 16:39:05</blue_time>
    <blue_class>javascript脚本</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> XML卷之实战锦囊(2):动态查询</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; bgcolor:#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=&#39;"+x+"&#39;]";
Layer1.innerHTML=source.documentElement.transformNode(stylesheet);
}
else {alert("请输入筛选条件!");} 
}
</script>
</head>
<body>
<p align="center"><span>XML卷之实战锦囊(2):动态查询</span></p>
<p id="Layer1" name="Layer1">
<xsl:apply-templates select="BlueIdea" />
</p>
<hr size="1" width="500" />
<table align="center" cellpadding="0" cellspacing="0" border="0" >
<tr>
<td>
<span >请输入筛选条件 : </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>编号</td>
<td>姓名</td>
<td>主题</td>
<td>发表时间</td>
<td>归类</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, I believe everyone will have no problem.
2) search.xsl is a format file, there are several things to pay attention to.
(1) In the script:
sortField=document.XSLDocument.selectNodes("//@select");
The function is: find all nodes with the attribute select. This is somewhat different from what I mentioned in dynamic sorting
sortField=document.XSLDocument.selectSingleNode("//@order-by");
. Please pay attention to this small difference and their respective functions.


sortField[1].value="team[blue_ID='"+x+"']";
So sortField[1] is the second node found and its corresponding node That is
9b92dc4fa42eb944186860fecb66172b

The parameter x is the value entered in the text box.
We modified the search condition of select="team" to select="team[blue_ID='x']"
The function is to add a judgment condition, and only the XML data whose blue_ID value is equal to x will be displayed.
Of course, you can enrich the conditions for judgment. The simple judgment I make here is to make it easier for everyone to understand.
Finally, the new sorted content is displayed by redisplaying the innerHTML value of Layer1.



(2) In the text:

select="team"
In my case it is sortField[1], but you may will change.
Then you must calculate accurately and make no mistakes, otherwise you will find another company!
I provide a commonly used method: in the code, you can use a loop to determine whether it is the node you need.


Another point:
XML has extremely strict requirements on upper and lower case. So if your writing is not standardized, it will catch a cold!                    

The above is the detailed content of XML Practical Cheats Volume 2: Dynamic Query. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn