473,503 Members | 3,308 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to use Xpath in Javascript to query XML?

1 New Member
I have an XML file (see below) linked to an XSL file (see below)

I'm trying to create an html file (see below) so that the user can query the XML and display only parts of it using the XSL provided

User may enter values as follows (examples):

for Author: "=Don Larson" or "contains Lar" or "contains Do" etc. or blank
for Title: "=The Game" or "contains The" or "contains Th"
or "contains am" etc. or blank
for Year: "=1987" or "<1993" or ">1984" etc.. or blank
for No of Authors: "=2" or "<3" or ">4" etc.. or blank

if everything is left blank, the whole table should be displayed
if sort is not selected, default sorting is applied
I'm trying to create a function that is called when the "display" button is clicked that takes all values (if any) inserted by the user in the form, and then evaluates them and manipulates them using XPATH expressions in order to display the table accordingly

unfortunately I don't know how to do this, I'm very confused..Any help?

XML file:

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0"?>
  2. <?xml-stylesheet type="text/xsl" href="books.xsl"?>
  3. <!DOCTYPE books [
  4. <!ENTITY Aring  "&amp;#197;" >
  5. <!ENTITY aring  "&amp;#229;" >
  6. <!ENTITY agrave "&amp;#224;" >
  7. <!ENTITY aacute "&amp;#225;" >
  8. <!ENTITY auml   "&amp;#228;" >
  9. <!ENTITY ccedil "&amp;#231;" >
  10. <!ENTITY eacute "&amp;#233;" >
  11. <!ENTITY egrave "&amp;#232;" >
  12. <!ENTITY iacute "&amp;#237;" >
  13. <!ENTITY iuml   "&amp;#239;" >
  14. <!ENTITY oacute "&amp;#243;" >
  15. <!ENTITY ocirc  "&amp;#244;" >
  16. <!ENTITY oslash "&amp;#248;" >
  17. <!ENTITY Ouml   "&amp;#214;" >
  18. <!ENTITY ouml   "&amp;#246;" >
  19. <!ENTITY uuml   "&amp;#252;" >
  20. ]>
  21. <books>
  22.  
  23. <scifi key="LarsSC85">
  24. <author>Don Larson</author>,
  25. <author>Jake Thomson</author>,
  26. <author>Robert Christian</author>,
  27. <title>Time Travels</title>,
  28. <pages>215-229</pages>,
  29. <year>1985</year>,
  30. <booktitle>TOM</booktitle>,
  31. </scifi>
  32.  
  33. <scifi key="LarsT86">
  34. <author>Jenny Jackson</author>,
  35. <author>Don Larson</author>,
  36. <title>Far Away</title>,
  37. <pages>240-251</pages>,
  38. <year>1986</year>,
  39. <booktitle>TOM</booktitle>,
  40. </scifi>
  41.  
  42. <scifi key="RogeA90">
  43. <author>Don Larson</author>,
  44. <title>Another Chance</title>,
  45. <pages>258-264</pages>,
  46. <year>1990</year>,
  47. <booktitle>TOM</booktitle>,
  48. </scifi>
  49.  
  50. </books>
XSL file:

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0"?>
  2. <xsl:stylesheet version="1.0" 
  3. xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  4.  
  5. <xsl:template match="/">
  6.   <html>
  7.   <body>
  8.     <center><h1>ScFi Books by TOM</h1>
  9.     <table border="1">
  10.     <tr>
  11.       <th>Title</th>
  12.       <th>Authors</th>
  13.       <th>Pages</th>
  14.       <th>Year</th>
  15.     </tr>
  16.       <xsl:for-each select="books/scifi">
  17.       <xsl:sort select="year"/>
  18.     <tr>
  19.       <td><center><xsl:value-of select="title"/></center></td>
  20.       <td>
  21.        <xsl:for-each select="author">
  22.         <p><xsl:value-of select="."/></p>
  23.         </xsl:for-each>
  24.       </td> 
  25.       <td><xsl:value-of select="pages"/></td>
  26.       <td><xsl:value-of select="year"/></td>
  27.     </tr>
  28.     </xsl:for-each>
  29.     </table>
  30.     </center>
  31.   </body>
  32.   </html>
  33. </xsl:template>
  34. </xsl:stylesheet> 
HTML file:

Expand|Select|Wrap|Line Numbers
  1. <html>
  2.  
  3. <script type="text/javascript">
  4.  
  5. var moz = (typeof document.implementation.createDocument != 'undefined');
  6. var ie =  (typeof window.ActiveXObject != 'undefined');
  7.  
  8. function loadXML(file) //function that loads xml/xsl files
  9. {
  10.   var xmlDoc;
  11.   if (moz)
  12.     xmlDoc = document.implementation.createDocument("", "", null);
  13.   else if (ie)
  14.     xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
  15.   xmlDoc.async = false;
  16.   xmlDoc.validateOnParse=false;
  17.   xmlDoc.load(file);
  18.   return xmlDoc;
  19. }
  20.  
  21. function filterTable(f) // function that modifies existing xsl 
  22. {                       // according to values entered by user in next form
  23.                         // and displays result underneath form
  24.     var xmlDoc = loadXML("books.xml");
  25.  
  26.     var stylesheet = loadXML("books.xsl");
  27.  
  28.     if (moz) // the following is just an incomplete example of the parser I want to use
  29.     {        // but I'm stuck when it comes to actually applying the values coming from the user
  30.              // to this
  31.       var nsResolver = stylesheet.createNSResolver(
  32.                        stylesheet.ownerDocument == null ?
  33.                        stylesheet.documentElement :
  34.                        stylesheet.ownerDocument.documentElement);
  35.       var value = stylesheet.evaluate(
  36.                        "//xsl:template[@match='...']//xsl:value-of.....",
  37.                        stylesheet, nsResolver,
  38.                        XPathResult.ANY_UNORDERED_NODE_TYPE, null);
  39.       value.singleNodeValue.setAttribute("select", "author");
  40.       var proc = new XSLTProcessor();
  41.       proc.importStylesheet(stylesheet);
  42.       var resultFragment = proc.transformToFragment(xmlDoc, document);
  43.       document.getElementById("target4").appendChild(resultFragment);
  44.     }
  45.     else if (ie)
  46.     {
  47.         var value = stylesheet.selectSingleNode(
  48.                 "//xsl:template[@match='item']//xsl:value-of");
  49.         value.setAttribute("select", "author");
  50.         document.write(xmlDoc.transformNode(stylesheet));
  51.     }
  52. }
  53.  
  54. </script>
  55.     <table border="1" cellpadding="8">
  56.  
  57.     <tr><td>
  58.     </br>
  59.     <form><b> search by </b>Authors: <input type="text" name="authors" />
  60.     Title: <input type="text" name="title" />Year: <input type="text" name="year" />
  61.     No of Authors: <input type="text" name="numauth" /></br></br>
  62.     <b> sort by</b>
  63.     <input type="radio" name="sorter" value="author" />author
  64.     <input type="radio" name="sorter" value="title" />title
  65.     <input type="radio" name="sorter" value="year" />year
  66.     <input type="radio" name="sorter" value="pages" />pages</br></br>
  67.     <input type="button" value="Display" onClick="filterTable(this.form)" align="center"/>
  68.     </form>
  69.     </td></tr>
  70.     </table>
  71. </html>
Mar 8 '12 #1
0 1985

Sign in to post your reply or Sign up for a free account.

Similar topics

1
3377
by: dima | last post by:
I'm using msxml3 xml looks like this: <a desc="&quot;B&quot;" /> how to query it in xpath? here's what i tried: a -- This works under XSL Patterns, but not under Xpath (using msxml 3) a --this...
6
1161
by: Michael | last post by:
To all, I am trying to query an XML document of the following structure. <ROOT> <A> <B id="1"/> <B id="2"/> <B id="3"/> </A>
3
2623
by: IMS.Rushikesh | last post by:
Hi Friends, I need a XPath query, whcih ll return me subset of data.... check below xml stream <label id="MyExpenseDetails_lbl" xlink:role="terseLabel">Short Expense Details</label> <label...
1
2002
by: Brad Hehe | last post by:
I am attempting to use XPath to query for a specific node in the sample XML I've provided below. I'm unsure of the exact XPath syntax, but I believe I need the following query to get the proper...
10
2271
by: Michael C# | last post by:
OK, here's the deal. I have a small XML file that represents a small database table. I load it into a System.XML.XMLDocument. So far so good. I run an XPath query against it to retrieve all the...
0
346
by: chandrika.tripathy | last post by:
I am new to XML. I have the following question. I have data stored in a relational database. I want to use XPATH to query it. I have a schema defined which represents the data in the database. Is...
3
1907
by: Greg | last post by:
Hi, I want to create a web based interface that uses a form + Javascript (in an XHTML namespace) to construct an XPath to query and modify the attributes of some SVG (in an SVG namespace). ...
3
2189
by: werD | last post by:
Hello I have an xml document that im currently using a forward only .net repeater on and using some xpath queries to display the data The xml is quite simple <?xml version="1.0"...
3
1593
by: ramabharti | last post by:
Hi, I do not know much about javascript. Basically I have a number of html pages located at my local disk. This is nothing related to client server concept. I want to search a string from all...
3
4122
by: 0to60 | last post by:
Please help! I'm using the following code to get an XML doc: string str = "http://api.local.yahoo.com/MapsService/V1/geocode?appid=12345&city=addison"; System.Net.HttpWebRequest request =...
0
7192
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7064
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7315
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7445
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5559
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4991
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3147
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1492
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.