Sorting data and how to sort it dynamically

Source: Internet
Author: User
Tags reflection sort xsl xsl stylesheet
Sorting data and how to sort it dynamically

Belltree
http://www.lurer.net/

Learning XML, a lot of mistakes, the master of the road a lot of correct point

Can be seen in <xsl:for-each select= "//item" order-by= "text ()" > and <xsl:apply-templates select= "//item"/>
Order-by property, which sorts the selected nodes according to the order-by value.

<singer>
<title>christina aguilera</title>
<songs>
<item id= "0" href= "Genieinabottle.christina.xml" >genie in a bottle</item>
<item id= "1" href= "Whatagirlwants.christina.xml" >what a girl wants</item>
<item id= "2" href= "Iturntoyou.christina.xml" >i turn to You</item>
<item id= "5" href= "Soemotional.christina.xml" >so emotional</item>
<item id= "4" href= "Comeonover.christina.xml" >come on over</item>
<item id= "3" href= "Reflection.christina.xml" >Reflection</item>
<item id= "6" href= "Lovefor.christina.xml" >love for all seasons</item>
<item id= "7" href= "Somebody.christina.xml" >somebody ' s somebody</item>
<item id= "Ten" href= "Puturhands.christina.xml" >when your put your hands on me</item>
<item id= "9" href= "Blessed.christina.xml" >Blessed</item>
<item id= "8" href= "Lovefindaway.christina.xml" >love'll find a way</item>
<item id= "One" href= "Obvious.christina.xml" >obvious</item>
</songs>
</singer>

In this example, if we need a list sorted by song name, we can use the following xsl:

<xsl:for-each select= "//item" order-by= "text ()" >
<a><xsl:attribute name= "href" ><xsl:value-of select= "@href"/></xsl:attribute><xsl: value-of/></a>
<br/>
</xsl:for-each>

This is sorted according to the value of each item node and can be sorted by using the id attribute, as long as the order-by= "text" is changed to Oder-by= "@id".

But if we need to let the user choose how to sort, or even not sort, in the original order.

It's time to get script and XML DOM to the field, and in the DOM, there's a series of methods and attributes that you can set up, you could regenerate a tree, and we can
To use these things to get rid of the value of the order-by attribute, and then reuse the XSL node to regenerate a tree for the node data you need, the tree is sorted
, note that the value of order-by is not the same as your new value. You can even remove the order-by attribute from the XSL node properties so that the resulting tree
is in the original order.

Look at the corresponding XML DOM Methods:
selectSingleNode Returns a single node
SetAttribute Set the property value, if the property does not exist, create it and set the value
RemoveAttribute Remove Attributes
Transformnode Returns a result tree after processing the node and its byte points using the appropriate XSL stylesheet

In the beginning, we're going to select the Xsl:for-each node in the XSL and give it a variable s to handle it:
var s = document. Xsldocument.selectsinglenode ("//xsl:for-each")

Then, the value of its property order-by is newly set:
SetAttribute ("order-by", key); Key is a variable and can be Id,text ()

Or, delete it:
RemoveAttribute ("order-by");

Haha, it's easy.

Now let's look at the part of the source tree that needs to be sorted, is each item node in the Singer/songs node, no need to select the entire tree, as long as singer/songs can
With a

var xmldoc = document. Xmldocument.selectsinglenode ("singer/songs");

Apply the entire XSL tree to the node:

divitems.innerhtml = Xmldoc.transformnode (document.  Xsldocument); Here's the mistake.

Is it necessary to apply the entire XSL tree to this node? Of course not, this will also bring errors, should be the result must be shown somewhere, we look back to see:
<xsl:template match= "/" >
<div id= "Divitems" >
<div id= "in" >

<xsl:for-each select= "//item" order-by= "id" >
<a><xsl:attribute name= "href" ><xsl:value-of select= "@href"/></xsl:attribute><xsl: value-of/></a>
<br/>
</xsl:for-each>

</div>
</div>
</xsl:template>
We are in <xsl:for-each> with <div> container, why use two <div>, this later explained, first look at Transformnode, apply the entire XSL
The consequences of the tree, which will regenerate a <div id= "Divitems" >...</div&gt, will result in two div with the same ID, and the result is not running.

We just need to select <div id= "in" > This node is enough.

var xsldoc = document. Xsldocument.selectsinglenode ("//div[@id = ' in ']");

And then apply the Xsldoc to the xmldoc.

divitems.innerhtml = Xmldoc.transformnode (xsldoc);

Two Div is still useful, the first is a container to display the results, the second is included in the result tree each time, if there is no <div id= "in" > Direct selection <div
Id= "Divitems" > will have the same error as applying the entire XSL tree.

Finally, or look at the complete: (Specific effects please see HTTP://GO8.163.COM/~BELLTREE/TEST.XML)
Xml:
<?xml version= "1.0"?>
<?xml-stylesheet type= "text/xsl" href= "test.xsl"?>

<singer>
<title>christina aguilera</title>
<songs>
<item id= "0" href= "Genieinabottle.christina.xml" >genie in a bottle</item>
<item id= "1" href= "Whatagirlwants.christina.xml" >what a girl wants</item>
<item id= "2" href= "Iturntoyou.christina.xml" >i turn to You</item>
<item id= "3" href= "Soemotional.christina.xml" >so emotional</item>
<item id= "4" href= "Comeonover.christina.xml" >come on over</item>
<item id= "5" href= "Reflection.christina.xml" >Reflection</item>
<item id= "6" href= "Lovefor.christina.xml" >love for all seasons</item>
<item id= "7" href= "Somebody.christina.xml" >somebody ' s somebody</item>
<item id= "8" href= "Puturhands.christina.xml" >when your put your hands on me</item>
<item id= "9" href= "Blessed.christina.xml" >Blessed</item>
<item id= "Ten" href= "Lovefindaway.christina.xml" >love'll find a way</item>
<item id= "One" href= "Obvious.christina.xml" >obvious</item>
</songs>
</singer>

Xsl:
<?xml version= "1.0" encoding= "gb2312"?>
<xsl:stylesheet xmlns:xsl= "Http://www.w3.org/TR/WD-xsl" >

<xsl:template match= "/" >

<script language= "JavaScript" >
<xsl:comment>
function sort (key) {
Find the "For-each" attributes in the style sheet.
var s = document. Xsldocument.selectsinglenode ("//xsl:for-each");

if (key== "")
S.removeattribute ("order-by");
Else
S.setattribute ("order-by", key);

Find the subset of the document we need to update.
var xmldoc = document. Xmldocument.selectsinglenode ("singer/songs");
var xsldoc = document. Xsldocument.selectsinglenode ("//div[@id = ' in ']");

Apply the style sheet to the subset, and update the display.
divitems.innerhtml = Xmldoc.transformnode (xsldoc);
}


</xsl:comment>
</script>
<body>
<table border= "1" cellspacing= "0" cellpadding= "1" >
<tr><td>
<div id= "Divitems" >
<div id= "in" >

<xsl:for-each select= "//item" order-by= "@id" >
<a><xsl:attribute name= "href" ><xsl:value-of select= "@href"/></xsl:attribute><xsl: value-of/></a>
<br/>
</xsl:for-each>

</div>
</div>
</td><td>
<input type= "button" value= "Text"/>
<input type= "button" value= "@id"/>
<input type= "button" value= "Origin"/>
</td></tr>
</table>
</body>
</xsl:template>
</xsl:stylesheet>


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.