XPath

Discussion in 'XML' started by Phani, Dec 9, 2004.

  1. Phani

    Phani Guest

    Hi all,

    I am new to XPath. I have a doubt in it.

    My xml is like this:

    <UserInfo>
    <Contacts>
    <Name>Chris Bob</Name>
    </Contacts>
    <Contacts>
    <Name>Mike Chris</Name>
    </Contacts>
    <Contacts>
    <Name>Chris</Name>
    </Contacts>
    <Contacts>
    <Name>Chrisine</Name>
    </Contacts>
    <Contacts>
    <Name>Kiran Kumar</Name>
    </Contacts>
    <Contacts>
    <Name>Kumar</Name>
    </Contacts>
    </UserInfo>

    So, i want to search for all names with the value in enter in html text
    box.

    If i enter Kumar then the last two nodes should come as it has kumar in
    it. Similarly if i enter Chris then first three nodes should display,
    but the fourth one should not come.
    Is this possible. Please help me on how to do it.

    --Phani
     
    Phani, Dec 9, 2004
    #1
    1. Advertising

  2. Phani wrote:


    > I am new to XPath. I have a doubt in it.
    >
    > My xml is like this:
    >
    > <UserInfo>
    > <Contacts>
    > <Name>Chris Bob</Name>
    > </Contacts>
    > <Contacts>
    > <Name>Mike Chris</Name>
    > </Contacts>
    > <Contacts>
    > <Name>Chris</Name>
    > </Contacts>
    > <Contacts>
    > <Name>Chrisine</Name>
    > </Contacts>
    > <Contacts>
    > <Name>Kiran Kumar</Name>
    > </Contacts>
    > <Contacts>
    > <Name>Kumar</Name>
    > </Contacts>
    > </UserInfo>
    >
    > So, i want to search for all names with the value in enter in html text
    > box.
    >
    > If i enter Kumar then the last two nodes should come as it has kumar in
    > it. Similarly if i enter Chris then first three nodes should display,
    > but the fourth one should not come.


    If you use the XPath
    /UserInfo/Contacts/Name[contains(., 'Kumar')]
    then all <Name> elements that contain the string 'Kumar' are found.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Dec 9, 2004
    #2
    1. Advertising

  3. Phani

    Joris Gillis Guest

    > Hi all,
    >
    > I am new to XPath. I have a doubt in it.
    >
    > My xml is like this:
    >
    > <UserInfo>
    > <Contacts>
    > <Name>Chris Bob</Name>
    > </Contacts>
    > <Contacts>
    > <Name>Mike Chris</Name>
    > </Contacts>
    > <Contacts>
    > <Name>Chris</Name>
    > </Contacts>
    > <Contacts>
    > <Name>Chrisine</Name>
    > </Contacts>
    > <Contacts>
    > <Name>Kiran Kumar</Name>
    > </Contacts>
    > <Contacts>
    > <Name>Kumar</Name>
    > </Contacts>
    > </UserInfo>
    >
    > So, i want to search for all names with the value in enter in html text
    > box.
    >
    > If i enter Kumar then the last two nodes should come as it has kumar in
    > it. Similarly if i enter Chris then first three nodes should display,
    > but the fourth one should not come.
    > Is this possible. Please help me on how to do it.

    Hi,

    This is a regular expression problem, regexes are not defined in XSLT1.0, but will be in XSLT2.0

    Meanwhile you can use extension, or work around it (in a quite ugly way):
    <xsl:apply-templates select="Contacts[contains(concat(' ',Name,' '),concat(' ',$name,' '))]"/>


    Or, alternatively, if you can, you should extend the XML structure: split the name node in a 'name' and 'surname' node.


    regards,
    --
    Joris Gillis (http://www.ticalc.org/cgi-bin/acct-view.cgi?userid=38041)
    Ceterum censeo XML omnibus esse utendum
     
    Joris Gillis, Dec 9, 2004
    #3
  4. Phani

    Phani Guest

    Hi Martin,

    I have tried your suggestion and it didn't worked out. It is popping up
    an error message saying :

    Unknown method.
    /UserInfo/Contacts/Name[-->contains(.<--,"Apple")]


    The following is the code which i have written:

    <html>
    <head>
    <style type="text/css">
    Body {font-family:Arial; }
    th {font-size:0.8em;}
    td {font-size:0.8em;}
    p {font-size:0.8em;}
    </style>
    </head>
    <body>

    <xml id="UserInfo" src="test.xml"></xml>

    <p>Enter Name: <input type='text' name=col_no style='border:1px solid
    #d2d2d2'> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input type=button name='Search' value='Search Now' style='border:1px
    solid black;height=20px'onClick='javascript:var sepvar
    = col_no.value;
    SetAndFilter(UserInfo,DataExtractLower,"UserInfo/Contacts/Name[contains(.,\"Apple\")]")'></p>

    <xml id="DataExtractLower"></xml>
    <table datasrc="#DataExtractLower" width=100% border=0>
    <thead bgcolor="#d2d2d2">
    <th>Name</th>
    <th>Location</th>
    <th>Phone</th>
    <th>Extn</th>
    <th>Title</th>
    <th>Department</th>
    </thead>
    <tr bgcolor="#e2e2e2">
    <td width="15%"><span datafld="Name"></span></td>
    <td width="15%"><span datafld="Location"></span></td>
    <td width="15%"><span datafld="Phone"></span></td>
    <td width="15%"><span datafld="Extn"></span></td>
    <td width="20%"><span datafld="Desig"></span></td>
    <td width="20%"><span datafld="DeptCC"></span></td>
    </tr>
    </table>

    <script language=javascript>
    function SetAndFilter (sourceXML, destXML, XPathFormula) {
    alert(XPathFormula);
    sourceXML.setProperty("SelectionLanguage", "XPath");
    var dummy = destXML.XMLDocument.loadXML(sourceXML.xml);
    vNodesToRemove = destXML.selectNodes(XPathFormula) ;
    vNodesToRemove.removeAll();
    }
    </script>

    </body>
    </html>

    So, whatever the user types in should check for the initial characters
    of both firstname and last name.

    If a user enters "cla" then it should bring back "Michael clark" as
    well as "clark michael"

    Hope my question is understandable. I learnt a way to program to
    display xml like html tables using data islands...

    --Phani
     
    Phani, Dec 9, 2004
    #4
  5. Phani wrote:


    > I have tried your suggestion and it didn't worked out. It is popping up
    > an error message saying :
    >
    > Unknown method.
    > /UserInfo/Contacts/Name[-->contains(.<--,"Apple")]


    Are you using IE 6? Only IE 6 comes with MSXML 3 which supports XPath
    1.0, earlier versions of IE come with an MSXML version that doesn't
    support XPath 1.0. So you either need to install IE 6 or you need to
    install MSXML 3 in so called replace mode.


    > <script language=javascript>
    > function SetAndFilter (sourceXML, destXML, XPathFormula) {
    > alert(XPathFormula);
    > sourceXML.setProperty("SelectionLanguage", "XPath");
    > var dummy = destXML.XMLDocument.loadXML(sourceXML.xml);
    > vNodesToRemove = destXML.selectNodes(XPathFormula) ;



    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Dec 9, 2004
    #5
  6. Phani

    Phani Guest

    I have installed IE6 installed Martin.

    Can you tell me on any other ways to do this type of things.

    I wanted to load all xml in single shot(when page loads) and then use
    the xml (or data islands) to search /filter.
    Please let me know on this...

    --Phani.
     
    Phani, Dec 9, 2004
    #6
  7. Phani wrote:

    > I have installed IE6


    Looking at your code again I think the problem is here:

    sourceXML.setProperty("SelectionLanguage", "XPath");
    var dummy = destXML.XMLDocument.loadXML(sourceXML.xml);
    vNodesToRemove = destXML.selectNodes(XPathFormula) ;

    You need to set the SelectionLanguage on destXML e.g.
    destXML.setProperty("SelectionLanuage", "XPath")

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Dec 9, 2004
    #7
  8. Phani

    Phani Guest

    Yep Martin, i have tried it and this time i didn't got any error.

    The javascript written in the code is wrong. It is working only if the
    xpath expression contains "!".

    like if name!="Apple" then destXML contains only the documents which
    have Apple in them.

    I need to change it. Can you help me on this. I think vNodesToRemove is
    where the problem is .
    Can we use "not" expression in XPATH.

    --Phani
     
    Phani, Dec 9, 2004
    #8
  9. Martin Honnen, Dec 9, 2004
    #9
  10. Phani

    Jeff Kish Guest

    On Thu, 09 Dec 2004 12:30:48 GMT, "Joris Gillis" <> wrote:

    >Ceterum censeo XML omnibus esse utendum

    What's the latin translate to? 'XML is for us all to enjoy'?
    Thanks
    Jeff Kish
     
    Jeff Kish, Dec 9, 2004
    #10
  11. Phani

    Joris Gillis Guest

    >> Ceterum censeo XML omnibus esse utendum
    > What's the latin translate to? 'XML is for us all to enjoy'?

    :) that would have been a nice slogan too...
    It means: "Furthermore, I'm of the opinion that XML should be used by everyone/for everything/to every purpose"

    At least that's what the translation ought to be. I'm not entirely sure if that's the precise translation, but grammatically it is correct or at least should be since I'm in the 6th Latin class.

    I'd also like to invent a slogan with W3C in it, but I've not enough inspiration...
    --
    Joris Gillis (http://www.ticalc.org/cgi-bin/acct-view.cgi?userid=38041)
    Ceterum censeo XML omnibus esse utendum
     
    Joris Gillis, Dec 9, 2004
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marvin_123456

    "Memory leak" in javax.xml.xpath.XPath

    Marvin_123456, Jul 29, 2005, in forum: Java
    Replies:
    4
    Views:
    1,974
    jan V
    Jul 29, 2005
  2. Alastair Cameron
    Replies:
    1
    Views:
    7,414
    SQL Server Development Team [MSFT]
    Jul 8, 2003
  3. Anna
    Replies:
    0
    Views:
    523
  4. goog
    Replies:
    0
    Views:
    508
  5. Tjerk Wolterink

    XPath: efficiency in xpath expressions

    Tjerk Wolterink, Nov 13, 2004, in forum: XML
    Replies:
    1
    Views:
    1,639
    Richard Tobin
    Nov 13, 2004
Loading...

Share This Page