4

次の XML ドキュメントがあります。

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

複数の画像を持つすべての本を選択する XPath 1.0 式を作成する方法は?

試してみinventory/book//image[2]/ancestor::bookましたが、間違った結果が得られました...

inventory/book//image[2]すべての本にすべての 2 番目の画像を指定しますか?

4

3 に答える 3

5

使用

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

bookこれにより、XMLドキュメントの最上位要素の子であり、2番目imageの子孫を持つすべての要素が選択されます。

この式は、通常、ドキュメント全体がトラバースされる//ため、で始まる式よりも潜在的に高速に評価されます。//

また、より効率的です

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

この式が。で始まらないように書き直されたとしても//

これはそうです。なぜなら、上記の式では、ソリューション内でcount(.//image)すべての子孫がカウントされるためです。image

(.//image)[2]

image2番目の子孫が存在することを確認するだけです。

最後に、XSLTベースの検証を次に示します。

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

この変換が提供されたXMLドキュメントに適用される場合:

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

XPath式が評価され、選択されたノード(この場合は1つだけ)が出力にコピーされます

<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>
于 2012-05-23T13:00:09.973 に答える
4

これで試してください

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

複数の画像タグがどこかにあるすべての本

于 2012-05-23T10:31:59.683 に答える
3

inventory/book//image[2]/image要素に 2 つ以上の子ノードがある場合、2 番目の画像を取得しますbook。試す:

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

階層を下ってbook要素に移動し、そこからクエリを開始する必要があります。述語 (または一般的な用語で言えばクエリ) はimage、そこからすべての要素を探すことです。これは、まさにdescendant軸が行うことです。特定の子孫を選択するには、すべての を検索するために::andを追加します。(関数の名前が示すように) が 1 より大きいかどうかを確認する場合の最後のテスト。nodenamedescendant::imageimagecount

于 2012-05-23T10:37:04.180 に答える