Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7990071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:54:22+00:00 2026-06-04T12:54:22+00:00

I have the following XML document : <?xml version=1.0 encoding=UTF-8?> <!DOCTYPE inventory SYSTEM books.dtd>

  • 0

I have the following XML document :

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

How to write an XPath 1.0 expression to select all books that have more then 1 image?

I tried inventory/book//image[2]/ancestor::book but it give wrong result …

is inventory/book//image[2] give all the 2nd image in every book ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T12:54:23+00:00Added an answer on June 4, 2026 at 12:54 pm

    Use:

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

    This selects all book elements that are children of the top element of the XML document and that have a second image descendant.

    This expression is evaluated potentially faster than any expression starting with //, because an expression starting with // typically causes the whole document to be traversed.

    It is also more efficient than:

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

    even if this expression was re-written not to start with //.

    This is so, because in the above expression count(.//image) causes all image descendants to be counted, while in our solution:

    (.//image)[2]
    

    only verifies that a second image descendant exists.

    Finally, here is an XSLT – based verification:

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

    when this transformation is applied to the provided XML document:

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

    the XPath expression is evaluated and the selected nodes (in this case just one) are copied to the output:

    <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>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following JSF table: <?xml version=1.0 encoding=UTF-8?> <!DOCTYPE html PUBLIC -//W3C//DTD XHTML
I have the following code. <?xml version=1.0 encoding=UTF-8 standalone=no?> <!DOCTYPE html PUBLIC -//W3C//DTD XHTML
I have the following string loaded to an XML document: <?xml version='1.0' encoding='utf-8'?> <soapenv:Envelope
I have an xml document in the following format. <?xml version=1.0 encoding=UTF-8 ?> <Rows>
I have an loaded an XML document with the following structure: <?xml version=1.0 encoding=UTF-8
I have the following XML document <?xml version='1.0' encoding='UTF-8'?><entry xmlns='http://www.w3.org/2005/Atom' xmlns:gd='http://schemas.google.com/g/2005' xmlns:issues='http://schemas.google.com/projecthosting/issues/2009' gd:etag='W/DEAERH47eCl7ImA9WhZTFEQ.'><id>http://code.google.com/feeds/issues/p/chromium/issues/full/921</id><published>2008-09-03T22:51:22.000Z</published><updated>2011-03-19T01:05:05.000Z</updated><title>Incorrect rendering</title><content
Consider we have the following xml document <?xml version=1.0 encoding=utf-8?> <Root> <Child /> </Root>
I have the following XML document: <?xml version=1.0 encoding=UTF-8?> <Offices id=0 enabled=false> <office />
I have an xml document with the following structure: <?xml version=1.0 encoding=UTF-8?> <items> <item>
I have this piece of html: <?xml version=1.0 encoding=UTF-8?> <!DOCTYPE html PUBLIC -//W3C//DTD XHTML

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.