2012-05-23 2 views
4

나는 다음과 같은 XML 문서가 : 의 XPath 1.0 체크 수

<?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> 

방법 1 이미자가 더있는 모든 책을 선택하는 XPath 1.0 표현을 쓸 수 있습니까?

inventory/book//image[2] 모든 책에있는 모든 2 차 이미지를주고 있습니다 ...

나는 inventory/book//image[2]/ancestor::book 시도했지만 잘못된 결과를 제공?

답변

5

사용는 :

/*/book[(.//image)[2]] 

이 XML 문서의 최상위 요소의 자식과 그 두 번째 image 후손있는 모든 book 요소를 선택합니다. //로부터 발현이 통상적으로 문서 전체가 이송되도록 때문에

이 식은, //로 시작하는 모든 표현보다 잠재적으로 더 빠르게을 평가한다.

또한보다 효율적입니다 :이 표현은 //로 시작하지 않는 재 작성되었습니다 경우에도

//book[count(.//image)>1] 

.

(.//image)[2] 

에만 제 image 자손이 있는지 확인 :

이는 우리의 용액 중에, 모든 자손 image 카운트되게 count(.//image) 상기 식에서, 그렇다.

마지막

은 여기 XSLT 인 - 기반 인증이 변환이 제공된 XML 문서에 적용

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:template match="node()|@*"> 
    <xsl:copy-of select="/*/book[(.//image)[2]]"/> 
</xsl:template> 
</xsl:stylesheet> 

: XPath 식을 평가

<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> 

및 선택한 노드 (이 경우 하나만)가 출력 :

로 복사됩니다.
<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> 
+2

최대 투표 ... 항상 배우기 좋음 –

+0

@ neu-rah : 환영합니다. –

4

//book[count(.//image)>1] 

으로 book 요소에 2 개 이상의 image 자식 노드가있는 곳

3

inventory/book//image[2]/ 두 번째 이미지를 제공 당신이 얻을 개 이상의 이미지 태그가있는 모든 책을보십시오. 보십시오 : 당신은 book 요소에 계층 구조를 아래로 이동 한 후 거기에에서 쿼리를 시작해야

//inventory/book[count(descendant::image) > 1] 

. 술어 (또는 평신도 용어로 쿼리)는 거기에서 모든 image 요소를 검색하는 것입니다. 정확히 축 번호 descendant입니다. ::nodename을 추가하여 descendant::image과 같이 특정 자손을 선택하면 image을 모두 찾을 수 있습니다. 마지막 테스트는 count (기능의 이름으로 제안 됨)이 1보다 큰지 여부를 확인합니다.