Help with XPath query

Hi

Given the following XML file, can anyone please help me figure out the XPath queries that perform the following two functions:

  1)  Select all xml nodes where <questionText admin="1">
  2)  Select all xml nodes where admin=0 or admin does not exist  (e.g. <questionText> )

I have this for the first but it doesn't work (used as part of an ASP.NET XPath query):     ds.XPath = "questions/question[@admin='1']"

Thanks.
<?xml version="1.0" encoding="utf-8" ?>
<questions>
	<question>
		<questionText>
			Lorem Ipsum
		</questionText>
		<answerText>
			<![CDATA[
			Lorem Ipsum
			]]>
		</answerText>
	</question>
	<question>
		<questionText admin="1">
			Lorem Ipsum
		</questionText>
		<answerText>
			<![CDATA[
			Lorem Ipsum
			]]>
		</answerText>
	</question>
</questions>

Open in new window

LVL 25
RouchieAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TimCotteeHead of Software ServicesCommented:
Hello Rouchie,

For the first one: .SelectNodes("questions/question[questionText/@admin='0'|questionText/@admin=null]")
For the second one: .SelectNodes("questions/question[questionText/@admin='0'] | questions/question[not(questionText/@admin)]")

Regards,

TimCottee
0
Kevin CrossChief Technology OfficerCommented:
Try like this:

ds.XPath = "questions/question[questionText/@admin = '1']"

For the second case, you can try just using the inverse.  @admin != '1'.  Typically you would test for the existence of an attribute by just using [questionText/@admin] as the test, but I can't remember if this will work; however, you can give it a try: [!questionText/@admin].  Think [questionText/@admin != '1'] will probably do the trick though.

Best regards,
Kevin
0
Kevin CrossChief Technology OfficerCommented:
I was too slow to hit the submit button. :)
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Kevin CrossChief Technology OfficerCommented:
Nice Tim.  I have been in Java code too much lately.  The ! is the NOT operator there.  You can try as Tim suggested like this:

First (Nodes with @admin = 1):
ds.XPath = "questions/question[questionText/@admin = '1']"

Second (Nodes with @admin = 0|null):
ds.XPath = "questions/question[not(questionText/@admin) | questionText/@admin = '0']"
<!-- or - still think this should work -->
ds.XPath = "questions/question[questionText/@admin != '1']"
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RouchieAuthor Commented:
Hi

Thanks very much for your responses.  Here's what I get:

@TimCottee

  >>For the first one: .SelectNodes("questions/question[questionText/@admin='0'|questionText/@admin=null]")

When I use  ds.XPath = "questions/question[questionText/@admin='0'|questionText/@admin=null]"  as in your example,  I get an error:

    Expression must evaluate to a node-set.


For the second one:

  >>.SelectNodes("questions/question[questionText/@admin='0'] | questions/question[not(questionText/@admin)]")

I used

   ds.XPath = "questions/question[questionText/@admin='0'] | questions/question[not(questionText/@admin)]"

And it works well.

@mwvisa1

  >> ds.XPath = "questions/question[questionText/@admin = '1']"

Works great.

  >> ds.XPath = "questions/question[not(questionText/@admin) | questionText/@admin = '0']"

Error thrown: Expression must evaluate to a node-set.

  >> ds.XPath = "questions/question[questionText/@admin != '1']"

Doesn't throw an error but returns no data.

Anyway between you both I think I've got the correct answers, so thanks again!
0
TimCotteeHead of Software ServicesCommented:
Rouchie,

Probably because I pasted the wrong bit for the first one!
It should simply be:

.SelectNodes("questions/question[questionText/@admin='1']")

TimCottee
0
Kevin CrossChief Technology OfficerCommented:
Yep, looks like you got it.  My second one was meant to be what you changed it to from Tim's as I was saying his suggestion was correct and just putting into context of ds.XPath and just botched it. :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.