1

I need to find value for a child attribute by giving values for one attibute at the same level and its parent at highere level.
Another requirement is
find value for a child attribute for which another attribute at same level with maximum value and given value for parent at higher level

Here is my XML

`<root>
  <ACTS>
    <ACT>Play</ACT>
    <A>
      <Day>1</Day>
      <time>Fri Feb 28 13:21:42 IST 2014</time>
    </A>
    <A>
      <Day>2</Day>
      <time>Fri Feb 28 13:21:43 IST 2014</time>
    </A>    
  </ACTS>
  <ACTS>
    <ACT>Study</ACT>
    <A>
      <Day>1</Day>
      <time>Fri Feb 28 13:21:42 IST 2014</time>
    </A>
    <A>
      <Day>2</Day>
      <time>Fri Feb 28 13:21:43 IST 2014</time>
    </A> 
    <A>
      <Day>3</Day>
      <time>Fri Feb 28 13:21:43 IST 2014</time>
    </A>   
  </ACTS>
</root>
`   

and the rquirement is to find the value for time attribute by giving values as Day=3 and ACT =Study

find time where ACT =Study and Day= maximum value under ACT=Study

I have tried the same with xpath and ended up with a mess (not able to retrieve values from Nodelist).

someone please guide me xquery and its usage in java for this..

which is best in this situation xquery or xpath.

Kuruvi
  • 45
  • 9

1 Answers1

4
let $x :=

<root>
<ACTS>
<ACT>Play</ACT>
<A>
  <Day>1</Day>
  <time>Fri Feb 28 13:21:42 IST 2014</time>
</A>
<A>
  <Day>2</Day>
  <time>Fri Feb 28 13:21:43 IST 2014</time>
</A>    
</ACTS>
<ACTS>
<ACT>Study</ACT>
<A>
  <Day>1</Day>
  <time>Fri Feb 28 13:21:42 IST 2014</time>
</A>
<A>
  <Day>2</Day>
  <time>Fri Feb 28 13:21:43 IST 2014</time>
</A> 
<A>
  <Day>3</Day>
  <time>Fri Feb 28 13:21:43 IST 2014</time>
</A>   
</ACTS>
</root>

XQuery -

let $maxDay := max($x//ACTS[ACT="Study"]/A/Day)
for $y in $x//ACTS[ACT/text()="Study"]/A[Day/text()=$maxDay]/time
 return $y

XPath -

//ACTS[ACT/text()="Study"]/A[Day/text()=max(//ACTS[ACT="Study"]/A/Day)]/time

You said - "someone please guide me xquery and its usage in java for this.. "

For this please refer following posts -

  1. How to read XML using XPath in Java

  2. Parsing XML with XPath in Java

Assuming your Java code is right, I think, your XPath expression put you in the mess. BTW, above links would guide you for the correctness of Java code.

You asked - "which is best in this situation xquery or xpath. "

Google for "Difference between XQuery and XPath", and you will get the answer.

HTH :)

Community
  • 1
  • 1
John
  • 2,820
  • 3
  • 30
  • 50
  • Thanks John...I forgot to mention one more requirement. i hav updated the question. for this only i'm getting some issues with Xpath.. Here `Day` is not known upfront. need o find the `maximum value for Day`. – Kuruvi Mar 12 '14 at 06:14
  • @Kuruvi - I too have updated my answer. If this is the correct answer then please accept :) – John Mar 12 '14 at 07:58
  • In the above output, if I want to get the time only i.e; 13:21:43, how to get it? – PranayStar Mar 12 '14 at 08:06
  • @John I realy apprecaite ur answer... But i'm using Xpath 1.0 . Xpath 1.0 doesn't support max function – Kuruvi Mar 12 '14 at 08:56
  • 2
    FWIW, the solution given is valid in XQuery 1.0 or XPath 3.0. It could be made valid XPath 2.0 by changing the "let" clause to an equivalent "for". – Michael Kay Mar 12 '14 at 09:01