How to use Xpath in Javascript to query XML?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • NewbieKid
    New Member
    • Mar 2012
    • 1

    #1

    How to use Xpath in Javascript to query XML?

    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:

    Code:
    <?xml version="1.0"?>
    <?xml-stylesheet type="text/xsl" href="books.xsl"?>
    <!DOCTYPE books [
    <!ENTITY Aring  "&amp;#197;" >
    <!ENTITY aring  "&amp;#229;" >
    <!ENTITY agrave "&amp;#224;" >
    <!ENTITY aacute "&amp;#225;" >
    <!ENTITY auml   "&amp;#228;" >
    <!ENTITY ccedil "&amp;#231;" >
    <!ENTITY eacute "&amp;#233;" >
    <!ENTITY egrave "&amp;#232;" >
    <!ENTITY iacute "&amp;#237;" >
    <!ENTITY iuml   "&amp;#239;" >
    <!ENTITY oacute "&amp;#243;" >
    <!ENTITY ocirc  "&amp;#244;" >
    <!ENTITY oslash "&amp;#248;" >
    <!ENTITY Ouml   "&amp;#214;" >
    <!ENTITY ouml   "&amp;#246;" >
    <!ENTITY uuml   "&amp;#252;" >
    ]>
    <books>
    
    <scifi key="LarsSC85">
    <author>Don Larson</author>,
    <author>Jake Thomson</author>,
    <author>Robert Christian</author>,
    <title>Time Travels</title>,
    <pages>215-229</pages>,
    <year>1985</year>,
    <booktitle>TOM</booktitle>,
    </scifi>
    
    <scifi key="LarsT86">
    <author>Jenny Jackson</author>,
    <author>Don Larson</author>,
    <title>Far Away</title>,
    <pages>240-251</pages>,
    <year>1986</year>,
    <booktitle>TOM</booktitle>,
    </scifi>
    
    <scifi key="RogeA90">
    <author>Don Larson</author>,
    <title>Another Chance</title>,
    <pages>258-264</pages>,
    <year>1990</year>,
    <booktitle>TOM</booktitle>,
    </scifi>
    
    </books>
    XSL file:

    Code:
    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    <xsl:template match="/">
      <html>
      <body>
        <center><h1>ScFi Books by TOM</h1>
        <table border="1">
        <tr>
          <th>Title</th>
          <th>Authors</th>
          <th>Pages</th>
          <th>Year</th>
        </tr>
          <xsl:for-each select="books/scifi">
          <xsl:sort select="year"/>
        <tr>
          <td><center><xsl:value-of select="title"/></center></td>
          <td>
           <xsl:for-each select="author">
            <p><xsl:value-of select="."/></p>
            </xsl:for-each>
          </td> 
          <td><xsl:value-of select="pages"/></td>
          <td><xsl:value-of select="year"/></td>
        </tr>
        </xsl:for-each>
        </table>
        </center>
      </body>
      </html>
    </xsl:template>
    </xsl:stylesheet>
    HTML file:

    Code:
    <html>
    
    <script type="text/javascript">
    
    var moz = (typeof document.implementation.createDocument != 'undefined');
    var ie =  (typeof window.ActiveXObject != 'undefined');
    
    function loadXML(file) //function that loads xml/xsl files
    {
      var xmlDoc;
      if (moz)
        xmlDoc = document.implementation.createDocument("", "", null);
      else if (ie)
        xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
      xmlDoc.async = false;
      xmlDoc.validateOnParse=false;
      xmlDoc.load(file);
      return xmlDoc;
    }
    
    function filterTable(f) // function that modifies existing xsl 
    {                       // according to values entered by user in next form
                            // and displays result underneath form
        var xmlDoc = loadXML("books.xml");
    
        var stylesheet = loadXML("books.xsl");
    
        if (moz) // the following is just an incomplete example of the parser I want to use
        {        // but I'm stuck when it comes to actually applying the values coming from the user
                 // to this
          var nsResolver = stylesheet.createNSResolver(
                           stylesheet.ownerDocument == null ?
                           stylesheet.documentElement :
                           stylesheet.ownerDocument.documentElement);
          var value = stylesheet.evaluate(
                           "//xsl:template[@match='...']//xsl:value-of.....",
                           stylesheet, nsResolver,
                           XPathResult.ANY_UNORDERED_NODE_TYPE, null);
          value.singleNodeValue.setAttribute("select", "author");
          var proc = new XSLTProcessor();
          proc.importStylesheet(stylesheet);
          var resultFragment = proc.transformToFragment(xmlDoc, document);
          document.getElementById("target4").appendChild(resultFragment);
        }
        else if (ie)
        {
            var value = stylesheet.selectSingleNode(
                    "//xsl:template[@match='item']//xsl:value-of");
            value.setAttribute("select", "author");
            document.write(xmlDoc.transformNode(stylesheet));
        }
    }
    
    </script>
        <table border="1" cellpadding="8">
    
        <tr><td>
        </br>
        <form><b> search by </b>Authors: <input type="text" name="authors" />
        Title: <input type="text" name="title" />Year: <input type="text" name="year" />
        No of Authors: <input type="text" name="numauth" /></br></br>
        <b> sort by</b>
        <input type="radio" name="sorter" value="author" />author
        <input type="radio" name="sorter" value="title" />title
        <input type="radio" name="sorter" value="year" />year
        <input type="radio" name="sorter" value="pages" />pages</br></br>
        <input type="button" value="Display" onClick="filterTable(this.form)" align="center"/>
        </form>
        </td></tr>
        </table>
    </html>
    Last edited by Dormilich; Mar 9 '12, 02:19 PM. Reason: Please use [CODE] [/CODE] tags when posting code.
Working...