locked
what is the 'xpath' query? RRS feed

  • Question

  • Hi,

     
             <emps>
                  <name>suresh</name>
                  <name>KRISHNA</name>
                  <name>tata</name>
                  <name>sudhakar</name>
            </emps>

             what is the "XPath" query for retrieving the employee name, that names should start with "su" ?

     pease help me

    Thursday, April 24, 2008 11:30 AM

Answers

  •  madhuSudhanReddyPulimi wrote:
    Hi,

     
             <emps>
                  <name>suresh</name>
                  <name>KRISHNA</name>
                  <name>tata</name>
                  <name>sudhakar</name>
            </emps>

             what is the "XPath" query for retrieving the employee name, that names should start with "su" ?

     pease help me

     

    First a few remarks:

     

    The established terminology is "XPath expression" that "selects" a "node" or a set of nodes, not query (used in DBMS, especially relational ones).

     

    Also, there is nothing as *the* query -- usually there exist many XPath expressions that select a given node.

     

     

    Now, having pointed out this, below is one of the many possible XPath expressions that selects all nodes named "name" such that its string value starts with the string "su":

     

      /*/*[starts-with(.,"su")]

     

    Some other possible XPath expressions are listed below:

     

     /*/name[starts-with(.,"su")]      (: will be necessary, if there are children of the top element not named "name Smile

     

     /emps/*[starts-with(.,"su")]      (: not necessary, because there is always only one top element in an xml documentSmile

     

     /*/child::*[starts-with(.,"su")]      (: not ommitting the axis (which defaults to "child::" Smile

     

      /*/*[substring(.,1,2) = "su"]      (:  another way to express the requirement that the string value must start with "su" Smile

     

     

    .   .   ..   .   ..   .   ..   .   ..   .   ..   .   ..   .   ..   .   ..   .   ..   .   ..   .   ..   .   ..   .   .

     

    etc., .... , etc.

     

    One XPath expression that selects the first (in document order) nodes named "name" such that its string value starts with the string "su":

     

      /*/*[starts-with(.,"su")][1]

     

     

    Thanks,

    Dimitre Novatchev

    Thursday, April 24, 2008 9:28 PM