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

  • Home
  • SEARCH
  • 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 999525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:22:04+00:00 2026-05-16T07:22:04+00:00

I have XML documents like: <rootelement> <myelement>test1</myelement> <myelement>test2</myelement> <myelement type=’specific’>test3</myelement> </rootelement> I’d like to

  • 0

I have XML documents like:

<rootelement>
<myelement>test1</myelement>
<myelement>test2</myelement>
<myelement type='specific'>test3</myelement>
</rootelement>

I’d like to retrieve the specific myelement, and if it’s not present, then the first one. So I write:

/rootelement/myelement[@type='specific' or position()=1]

The XPath spec states about the ‘or expression’ that:

The right operand is not evaluated if
the left operand evaluates to true

The problem is that libxml2-2.6.26 seems to apply the union of both expressions, returning a “2 Node Set” (for example using xmllint --shell).

Is it libxml2 or am I doing anything wrong ?

  • 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-05-16T07:22:05+00:00Added an answer on May 16, 2026 at 7:22 am

    Short answer: your selector doesn’t express what you think it does.


    The or operator is a union.

    The part of the spec you quoted (“The right operand is not evaluated…”) is part of standard boolean logic short circuiting.

    Here’s why you get a 2-node set for your example input: XPath looks at every myelement that’s a child of rootelement, and applies the [@type='specific' or position()=1] part to each such node to determine whether or not it matches the selector.

    1. <myelement>test1</myelement> does not match @type='specific', but it does match position()=1, so it matches the whole selector.
    2. <myelement>test2</myelement> does not match @type='specific', and it also does not match position()=1, so it does not match the whole selector.
    3. <myelement type='specific'>test3</myelement> matches @type='specific' (so XPath does not have to test its position – that’s the short-circuiting part) so it matches the whole selector.

    The first and last <myelement>s match the whole selector, so it returns a 2-node set.

    The easiest way to select elements the way you want to is to do it in two steps. Here’s the pseudocode (I don’t know what context you’re actually using XPath in, and I’m not that familiar with writing XPath-syntax selectors):

    1. Select elements that match /rootelement/myelement[@type='specific']
    2. If elements is empty, select elements that match /rootelement/myelement[position()=1]
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some XML that looks something like this: <Root> <Documents> <Document id=1/> </Documents>
During development, I'd like to have an XML file in my iPhone app's Documents
I have some xml that looks like this: <rootElement attribute=' > '/> This is
I have an XML document something like ::: <?xml version=1.0 encoding=utf-8?> <?mso-application progid=Excel.Sheet?> <Workbook
Say I have a xml document that looks like this <foo> <bar id=9 />
I have a simple xml document that looks like the following snippet. I need
I have a complete XML document in a string and would like a Document
In my Python app, I have an XML document that I'd like to transform
I am writing an XML document in C#. I have something like this... string
I have an application with a REST style interface that takes XML documents via

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.