2012-05-21 3 views
0

나는이 XML 파일을 가지고, 나는 이러한 쿼리 찾으려 : 사진 하나 개 이상의 사진이 XPath 쿼리 - 부울 조건은

  • 모든 도서를 포함

    1. 모든 책을
    2. 모든

      <?xml version="1.0" encoding="UTF-8"?> 
      <!DOCTYPE inventory SYSTEM "books.dtd"> 
      <inventory> 
          <book num="b1"> 
           <title>Snow Crash</title> 
           <author>Neal Stephenson</author> 
           <publisher>Spectra</publisher> 
           <price>14.95</price> 
           <chapter> 
            <title>Snow Crash - Chapter A</title> 
            <paragraph> 
             This is the <emph>first</emph> paragraph. 
             <image file="firstParagraphImage.gif"/> 
             afetr image... 
            </paragraph> 
            <paragraph> 
             This is the <emph>second</emph> paragraph. 
             <image file="secondParagraphImage.gif"/> 
             afetr image... 
            </paragraph> 
           </chapter> 
           <chapter> 
            <title>Snow Crash - Chapter B</title> 
            <section> 
             <title>Chapter B - section 1</title> 
             <paragraph> 
              This is the <emph>first</emph> paragraph of section 1 in chapter B. 
              <image file="Chapter_B_firstParagraphImage.gif"/> 
              afetr image... 
             </paragraph> 
             <paragraph> 
              This is the <emph>second</emph> paragraph of section 1 in chapter B. 
              <image file="Chapter_B_secondParagraphImage.gif"/> 
              afetr image... 
             </paragraph> 
            </section> 
           </chapter> 
           <chapter> 
            <title>Chapter C</title> 
            <paragraph> 
             This chapter has no images and only one paragraph 
            </paragraph> 
           </chapter> 
          </book> 
          <book num="b2"> 
           <title>Burning Tower</title> 
           <author>Larry Niven</author> 
           <author>Jerry Pournelle</author> 
           <publisher>Pocket</publisher> 
           <price>5.99</price> 
           <chapter> 
            <title>Burning Tower - Chapter A</title> 
           </chapter> 
           <chapter> 
            <title>Burning Tower - Chapter B</title> 
            <paragraph> 
             This is the <emph>second</emph> paragraph of chapter B in the 2nd book. 
             <image file="Burning_Tower_Chapter_B_secondParagraphImage.gif"/> 
             afetr image... 
            </paragraph> 
           </chapter> 
          </book> 
          <book num="b3"> 
           <title>Zodiac</title> 
           <author>Neal Stephenson</author> 
           <publisher>Spectra</publisher> 
           <price>7.50</price> 
           <chapter> 
            <title>Zodiac - Chapter A</title> 
           </chapter> 
          </book> 
          <!-- more books... --> 
      </inventory> 
      
      0,123,516 : "래리 니븐"로 책을 쓴 저자는
    3. 여기

    은 XML이다

    그리고 여기 내 답변입니다 :

    1. /inventory/book[count(image)=1]/title 
    2. /inventory/book[count(image)>=1/title 
    3. /inventory/book[author=”Larry Niven”][count(author)>1]/author 
    

    하지만 자바 XPath는 코드이 답변을 테스트 할 때, 그것은 작동하지 않습니다.

    어디에서 실수가 있습니까? 감사

  • 답변

    2

    첫 번째 두 image 문서에서입니다 관련된 문제가이 - 필요가 현재 노드에서 검색하고 모든 어린이 (뿐만 아니라 직접 아이들) 검색 :

    /inventory/book[count(.//image) = 1]/title 
    /inventory/book[count(.//image) >= 1]/title 
    

    귀하의 마지막 문제에있다가 따옴표 (작은 따옴표 사용 필요) 및 Larry Niven을 중복하십시오.

    /inventory/book[author='Larry Niven'][count(author) > 1]/author[. != 'Larry Niven'] 
    

    참고 대신 and를 사용할 수 있습니다 :

    /inventory/book[author='Larry Niven' and count(author) > 1]/author[. != 'Larry Niven'] 
    
    다음은 수정입니다