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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:28:40+00:00 2026-05-28T02:28:40+00:00

I use this xpath expression to list all NON TEST items: /Items/Item[State!=’TEST’]/Name Normally input

  • 0

I use this xpath expression to list all “NON TEST” items:

/Items/Item[State!='TEST']/Name

Normally input XML looks like this and everything works fine:

<Items>
  <Item>
    <Name>Item1</Name>
    <State>ACTIVE</State>
  </Item>
  <Item>
    <Name>Item2</Name>
    <State>TEST</State>
  </Item>
</Items>

But when Item miss State element the xpath expression selects nothing:

<Items>
  <Item>
    <Name>Item1</Name>
  </Item>
  <Item>
    <Name>Item2</Name>
  </Item>
</Items>

Please, how should I change my xpath expression so that it also works when State element does not exists? If State element does not exists Item is considered as “NON TEST” item. I have no option to change xml structure.

  • 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-28T02:28:40+00:00Added an answer on May 28, 2026 at 2:28 am

    Use the following expression:

    /Items/Item[not(State='TEST')]/Name
    

    The difference is that State!='TEST' selects those items having any State element whose string value is not equal to TEST, while not(State='TEST') selects all items not having a State element whose string value is equal to TEST (whether State exists or not). It’s a subtle but significant difference.

    From the spec:

    If one object to be compared is a node-set and the other is a string,
    then the comparison will be true if and only if there is a node in the
    node-set such that the result of performing the comparison on the
    string-value of the node and the other string is true.

    Furthermore, imagine the following input:

    <Items>
        <Item>
            <Name>Item1</Name>
            <State>blah</State>
            <State>TEST</State>
        </Item>
        <Item>
            <Name>Item2</Name>
            <State>TEST</State>
        </Item>
    </Items>
    

    This expression:

    /Items/Item[State!='TEST']/Name
    

    …will select the first Item‘s Name element, because it contains at least one State element not having a value of TEST.

    However, this expression:

    /Items/Item[not(State='TEST')]/Name
    

    …selects no items. This second form is very often what you want.

    See this note from the spec:

    NOTE: If $x is bound to a node-set, then $x="foo" does not mean the
    same as not($x!="foo"): the former is true if and only if some node in
    $x has the string-value foo; the latter is true if and only if all
    nodes in $x have the string-value foo.

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

Sidebar

Related Questions

In this table how would I use YQL/Xpath to get all data members of
After executing a XPath expression for extracting all year and value elements associated with
Is this xpath a valid XPath expression? (It does what it should ). #!/usr/bin/env
I'd like to use the value of an xslt parameter in an xpath expression.
I'm trying to use XPath to find all elements that have an element in
I have a standard meta tag like this: <meta name=desc content=test/> That I want
I have an XML document structured as follows <items> <item> <name>item1</name> <attributes>a,b,c,d</attributes> </item> <item>
Consider: <Envelope> <whichNode>node2</whichNode> <node1>Hello</node1> <node2>World</node2> </Envelope> Can I use a single XPath expression to
use this website a lot but first time posting. My program creates a number
I use this tool called Lazy C++ which breaks a single C++ .lzz file

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.