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 8757647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:21:17+00:00 2026-06-13T14:21:17+00:00

I am quite new to XPath so bear with me. I have a XPath

  • 0

I am quite new to XPath so bear with me. I have a XPath expression

'.//*[contains(.,"Obama")]/text()'

that gets me the text that contains “Obama”. However, I haven’t been able to figure out how to add

and [not(contains(., "Romney"))] to the expression without getting a syntax error. How is it done? Help much appriciated!

  • 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-13T14:21:18+00:00Added an answer on June 13, 2026 at 2:21 pm

    Use:

    .//*[contains(.,"Obama") and not(contains(.,"Romney"))]/text()
    

    XSLT – based verification:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output method="text"/>
    
     <xsl:template match="/">
         <xsl:copy-of select=
         './/*[contains(.,"Obama") and not(contains(.,"Romney"))]/text()'/>
     </xsl:template>
    </xsl:stylesheet>
    

    When this transformation is applied on the following XML document:

    <election>
     <choice>Maybe  Obama</choice>
     <choice>Maybe Romney</choice>
    </election>
    

    the XPath expression is evaluated and the selected node is copied to the output:

    Maybe  Obama
    

    Do note:

    SomeExpression[x][y]
    

    is not always equivalent to:

    SomeExpression[x and y]
    

    Therefore, it is recommended the latter — not the former, as specified in the answer by @ChrisGerken.

    Here is a concrete example:

    Let’s have this XML document:

    <nums>
      <num>01</num>
      <num>02</num>
      <num>03</num>
      <num>04</num>
      <num>05</num>
      <num>06</num>
      <num>07</num>
      <num>08</num>
      <num>09</num>
      <num>10</num>
    </nums>
    

    and these two XPath expressions:

    /*/*[. mod 3 = 0 and position() = 3]
    

    and

    /*/*[. mod 3 = 0][position() = 3]
    

    The first expression selects:

    <num>03</num>
    

    However, the second expression selects:

    <num>09</num>
    

    And here is a complete 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:template match="/">
         <xsl:copy-of select=
         "/*/*[. mod 3 = 0 and position() = 3]"/>
         ================
         <xsl:copy-of select=
         "/*/*[. mod 3 = 0][position() = 3]"/>
    
     </xsl:template>
    </xsl:stylesheet>
    

    When this transformation is applied on the above XML document, the two XPath expressions are evaluated and the results of these evaluations are copied to the output:

    <num>03</num>
    ================
    <num>09</num>
    

    Explanation:

    position() is a *context-sensitive` function and typically produces different results when used in the k-th and in the m-th predicate, where k != m

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im quite new at Javascript however I have been trying to create a currency
Quite new to this but i have created a SQL script that groups by
I am quite new to mongoid and rails. So I have some troubles to
I am new to XPath, and from what I have read in some tutorials
Quite new to this mocking thing, I have a couple of questions. Correct me
Im quite new to Java and i have stumbled upon the following problem. Im
I'm quite new to Linq and have had some problems updating my dabase. I
I'm quite new to that functional programming paradigm, but so far I like it.
Quite new to RIA Services but i am progressing... I noticed that some methods
Quite new to Solr 1.4 - seems to be very powerful indeed. However, I

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.