• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 295
  • Last Modified:

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

0
Rouchie
Asked:
Rouchie
  • 4
  • 2
2 Solutions
 
TimCotteeCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now