|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
dynamic use of XSLT to implement search function
Hi,
Is it possible to use XSLT in the following context? I would like to search XML file and display information on the returned nodes. This is best illustrated by example: <library> <book> <title>a first book</title> <author>a first author</author> </book> <book> <title>a second book</title> <author>a second author</author> </book> <book> <title>a second book</title> <author>a first author</author> </book> </library> I would like to offer a search facility in an html page, that will for example traverse the tree and output the details for a specific author. Could this be achieved by passing in an XPath expression as a parameter? Any other suggestions would be most welcome. Thanks for any help, Dave |
|
#2
|
||||
|
||||
|
I have been working on a cd based project doing exactly this for the past 2 weeks. I created a function called show that gets passed the xml & xsl files. In this case i actually modified the xslt_process function to use a value from the arguements variable ('arg:/_xsl')
PHP Code:
What i did is use file & join to load file and then the assign to arguements after doing an strreplace on file PHP Code:
In the XSL file i had key words/variables which I replaced with the xsl search tags then. Once all that was done i ran the show function. PHP Code:
If any of this doesnt make sense please let me know and i will try and explain it better. |
|
#3
|
|||
|
|||
|
Hi Andreas,
Firstly thank you for you quick reply. I'm not sure I completely understand what you have achieved, however, I think I get the basic idea. Is this correct? You are changing the XSLT templates on the fly via PHP file handling? I have no experience of PHP and it's not really suitable for my purpose but as I am working with local based files, I think I could implement something similar in JavaScript. Do you have a working prototype or demo that I could see? Or even an example XSLT file? Your help is really appreciated, Dave |
|
#4
|
||||
|
||||
|
Thats right... i am loading the basic XSL and then changing it as needed. I am not sure how you would achieve the same result via javascript. You might be able to do it via XSL variables but i dont know for sure.
|
|
#5
|
|||
|
|||
|
thanks again Andreas,
I will look into the methods you suggested. Could you show me a sample XSLT template or just the XPath expression that searches on a cd title for example? That would be of great use. Thanks again, Dave |
|
#6
|
||||
|
||||
|
I use the <xsl:for-each> tag and then just pass an extra option to it. <xsl:for-each select="People[FirstName='Mary']">
This would display all people that have the first name of Mary. If you want to learn XSL i would suggest the XSL tutorials from Devshed. I only learnt XSL a week ago and found everything by searching the web. There is heaps of tutorials out there http://www.devshed.com/Server_Side/...sics/XSLBasics1 http://www.devshed.com/Server_Side/...sics/XSLBasics2 |
|
#7
|
|||
|
|||
|
Hi Andreas,
Sorry for the delay in thanking you, I thought I had posted a reply but obviously forgot. Just to let you know that I found, what I think is a more elegant solution to the problem. It involves using JavaScript to query the xml file using XPath expressions. The code below queries the XML file on artist and returns all of a NodeList of all the Al Green Tracks. The data is then outputted to a table. <SCRIPT LANGUAGE="JavaScript"> <!-- var xmlDoc = new ActiveXObject("Msxml2.DOMDocument"); xmlDoc.async = false; xmlDoc.load("sample.xml"); //xmlDoc.setProperty("SelectionLanguage", "XPath"); function ex1(query) { var matchedNodes = xmlDoc.childNodes[1].selectNodes(query); document.writeln("<table border='1'>"); for (var i=0; i< matchedNodes.length; i++) { document.writeln("<tr>"); for (var j=0; j< matchedNodes[i].childNodes.length; j++) { document.writeln("<td>"+matchedNodes[i].childNodes[j].text+"</td>"); if (j == matchedNodes.length -1) document.writeln("</tr>"); } } document.writeln("</table>"); } var query = "track[artist='Al Green']"; ex1(query); // --> </SCRIPT> This may be of use to you in the future. Regards, Dave |
|
#8
|
||||
|
||||
|
When i was doing my research i did see that solution you posted. The one main problem with it is that it will only work in internet explorer since it uses ActiveX. Also you need to ensure that your browser is of the right version to. In a web based situation this is not possible and therefore that system is useless. Thats why its better doing it server sided.
If its for an intranet situation or a controlled environment then the solution will be fine. |
|
#9
|
|||
|
|||
|
yeah, I'm using it in a "non-web" based application. Although a cross-browser implementation would be nice, it is not a priority in my situation.
best, Dave |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > XML Programming > dynamic use of XSLT to implement search function |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|