Thursday, April 29, 2010

Set operators in XPath

What's this? A post? Fer realz? Best pretend it never happened.

Meanwhile, I'll just continue typing. About set operators in XPath, no less.

In the olden days of XPath 1.0, there was only one set operator, "|", which performed a union on node sets. So you could use an expression like "//foo | //bar" to select all foo and bar elements. If, however, you wanted the intersection or difference of two node sets, you were out of luck, as there were no dedicated operators to help you do that.

Fortunately, the much improved XPath 2.0 remedies that situation by introducing the "intersect", and "except" operators. (And "union", which is just another way of saying "|".) So now you can do "foo intersect bar" to get nodes that are both foo's and bars, and "foo except bar" to get nodes that are foo's but not bars.

Sounds great, doesn't it? Sadly, there's a big pitfall in the way that nodes are determined to be equal. You see, for purposes of these operators, nodes are only deemed equal if they are the exact same nodes, not if they merely have the same name and the same content. So, in the document below, the two foo elements are not considered equal!

<root>
<foo>This is a foo.</foo>
<foo>This is a foo.</foo>
</root>


Now what if we want to perform set operations on nodes based on equality in terms of name and content, rather than XPath's strict definition?

Fortunately, XPath does come with a function that can determine if two nodes are identical, without taking into account if they're the exact same node. We can use this to write queries that do what we want.

Let's assume we have a variable a containing the following nodes (under a nameless root, not as a sequence):

<e></e>
<f>fff</f>
<g></g>


As well as a variable b containing these nodes:

<e></e>
<f>fff</f>
<i></i>


We can now simulate "except" as follows:

$a/*[empty(for $b1 in $b/* return (if (deep-equal(.,$b1)) then (true()) else ()))]

You should read this as: for all nodes of a, return only those for which no corresponding node can be found in b.

If you understand how our "except" works, you'll have no problem understanding "intersect":

$a/*[exists(for $b1 in $b/* return (if (deep-equal(.,$b1)) then (true()) else ()))]

Read this as: for all nodes of a, return only those for which a corresponding node can be found in b.

Finally, we have "union":

$a/*[empty(for $b1 in $b/* return (if (deep-equal(.,$b1)) then (true()) else ()))],$b/*

Read this as: take a except b and add all of b. (The "except b" bit is necessary to make sure you don't get the nodes that occur in both variables twice. Without it, you'd end up with duplicate <e> and <f> elements.)

There's one thing to be aware of in these queries though. Unlike the set operators, these queries don't remove duplicates when these are already in either of the inputs. So if a contains duplicate nodes, "$a except $b" will remove these, while our query won't. Depending on your use case this is either a bug or a feature. :-)

To help you out, here's a way to remove duplicates from a. If you combine this with the queries above, you'll have something that behaves just like the set operators do. (Apart from the difference in determining when nodes are equal, of course.)

$a/*[empty(for $a1 in subsequence($a/*,1,position()-1) return (if (deep-equal(.,$a1)) then (true()) else ()))]

Read this as: go through all nodes in a and return a node only if we haven't already passed an identical one.

There ya go. Hope this'll be of some help.

131 comments:

Anonymous said...

xanax online good place buy xanax online - xanax wikipedia pl

Anonymous said...

buy tramadol online tramadol 100mg euphoria - tramadol hcl 50 mg strong

Anonymous said...

generic xanax xanax drug prescribed - xanax 106 s yellow pill

Anonymous said...

alprazolam buy what do generic xanax pills look like - xanax dosage generic

Anonymous said...

buy tramadol online tramadol 50 mg dog - tramadol 50 mg side effects for dogs

Anonymous said...

generic xanax how to buy xanax online legally - xanax bars how many mg

Anonymous said...

xanax order no prescription xanax dosage overdose - xanax dosage klonopin

Anonymous said...

tramadol without prescription where can i buy tramadol usa - buy tramadol online florida

Anonymous said...

generic xanax xanax bars withdrawal symptoms - xanax overdose vomiting

Anonymous said...

tramadol online pharmacy tramadol 50 mg 377 - ultram 50 mg tablet

Anonymous said...

buy tramadol online tramadol 650 om - tramadol no prescription mastercard

Anonymous said...

xanax no prescription buy xanax online now - xanax effects

Anonymous said...

xanax online xanax dosage mayo clinic - what does a 1mg xanax look like

Anonymous said...

carisoprodol 350 mg carisoprodol glaucoma - carisoprodol zyrtec

Anonymous said...

buy tramadol online gettin high on tramadol - tramadol for dogs loss of appetite

Anonymous said...

buy tramadol online tramadol 50mg over counter - tramadol for dogs and humans the same

Anonymous said...

carisoprodol 350 mg carisoprodol over the counter - buy carisoprodol cheap

Anonymous said...

buy tramadol without a script buy tramadol online usa cheap - buy tramadol cheap online

Anonymous said...

generic xanax xanax drug long system - xanax side effects shaking

Anonymous said...

generic xanax xanax side effects nausea - question xanax 2 overdose

Anonymous said...

buy tramadol online tramadol withdrawal kratom - tramadol withdrawal rash

Anonymous said...

buy tramadol cheap online tramadol 50 mg efectos secundarios - tramadol dosage men

Anonymous said...

generic xanax how many 2mg xanax to overdose - half 1mg xanax

Anonymous said...

buy tramadol online tramadol 50 mg cheap - buy tramadol in europe

Anonymous said...

buy tramadol cod tramadol 50mg what is it for - 50mg tramadol high dose

Anonymous said...

xanax online xanax pills order - buy xanax online insurance

Anonymous said...

buy carisoprodol carisoprodol vs metaxalone - erowid tramadol carisoprodol

Anonymous said...

xanax online xanax drug urine test - xanax gg 256 side effects

Anonymous said...

online xanax no prescription side effects of xanax 0.5mg - xanax dosage daily

Anonymous said...

buy carisoprodol online no prescription carisoprodol 350 mg controlled substance - can carisoprodol 350 mg get you high

Anonymous said...

generic xanax yellow xanax bars r039 - xanax drug test detection period

Anonymous said...

generic xanax alprazolam xr 2mg tablets - alprazolam 0 5mg eurofarma

Anonymous said...

carisoprodol 350 mg what is soma carisoprodol used for - carisoprodol 175 mg

Anonymous said...

order tramadol buy tramadol online dogs - tramadol dosage back pain

Anonymous said...

buy tramadol online buy tramadol online from usa - buy tramadol pay cod

Anonymous said...

buy carisoprodol carisoprodol soma scheduled drug - carisoprodol 350 mg picture

Anonymous said...

cheap generic cialis buy brand cialis online us - cialis online usa

Anonymous said...

buy tramadol online buy tramadol online mastercard - tramadol dosage rat

Anonymous said...

cialis online buy cialis online us - buy cialis no prescription australia

Anonymous said...

cialis no prescription can i buy cialis over the counter - cialis daily work

Anonymous said...

buy tramadol online tramadol online no prescription mastercard - buy tramadol online no prescription usa

Anonymous said...

buy cialis online online apotheke cialis 20mg - review of cialis daily

Anonymous said...

buy tramadol online tramadol for dogs max dose - tramadol yellow and green capsule

Anonymous said...

cialis online generic cialis online kaufen - buy 200 mg cialis

Anonymous said...

buy cialis online cialis 20mg price us - order cialis professional online

Anonymous said...

xanax online buy xanax cod - xanax weed

Anonymous said...

buy cialis how to order cialis - generic cialis us suppliers

Anonymous said...

generic xanax xanax on employment drug test - does xanax show up same klonopin drug test

Anonymous said...

cialis online cialis online pharmacy us - cialis sample coupon

Anonymous said...

buy cialis online generic cialis tadalafil 20mg reviews - cialis make you last longer

Anonymous said...

xanax online xanax and alcohol how much - xanax dosage to get you high

Anonymous said...

buy cialis super active buy cialis online with mastercard - generic cialis 20

Anonymous said...

xanax online pharmacy what is generic xanax - generic xanax pictures mg

Anonymous said...

cialis no prescription buy cialis miami - cheap genuine cialis

Anonymous said...

buy tramadol next day tramadol addiction dogs - effects of tramadol addiction

Anonymous said...

buy tramadol can take 2 tramadol 50 mg - tramadol withdrawal breathing

Anonymous said...

http://landvoicelearning.com/#97734 order tramadol online to florida - tramadol dosage cats

Anonymous said...

http://landvoicelearning.com/#97734 cheap tramadol without prescriptions - buytramadol-online

Anonymous said...

buy tramadol online tramadol addiction gaza - tramadol 50 mg kern pharma

Anonymous said...

buy tramadol usa can you buy tramadol online in usa - can take 2 tramadol 50 mg

Anonymous said...

generic tramadol tramadol hcl sleep - get high tramadol hcl 50mg

Anonymous said...

learn how to buy tramdadol tramadol 50 mg get high - tramadol withdrawal st john's wort

Anonymous said...

http://landvoicelearning.com/#74967 tramadol order online tramadol 50mg - usual dosage tramadol

Anonymous said...

learn how to buy tramdadol order tramadol no prescription overnight - tramadol hcl an 627

Anonymous said...

http://landvoicelearning.com/#38471 tramadol zydus - buy tramadol online with mastercard

Anonymous said...

tramadol no rx tramadol depression - tramadol hcl versus hydrocodone

Anonymous said...

http://buytramadolonlinecool.com/#30807 tramadol 50 mg how strong - tramadol 325 mg high

Anonymous said...

buy tramadol online no prescription cheap tramadol for dogs generic - tramadol addiction message board

Anonymous said...

buy tramadol pain killer tramadol 50mg - tramadol 50mg for pets

Anonymous said...

http://blog.dawn.com/dblog/buy/#56932 cheap tramadol without prescriptions - buy online texas tramadol

Anonymous said...

http://buytramadolonlinecool.com/#30807 legit online pharmacy tramadol - can you buy tramadol in usa

Anonymous said...

buy tramadol with paypal tramadol for dogs liver - buy tramadol online no prescription needed

Anonymous said...

buy tramadol online order cheap tramadol-overnight - tramadol addiction side effects

Anonymous said...

http://blog.dawn.com/dblog/buy/#91875 tramadol hcl usa - tramadol zyrtec interaction

Anonymous said...

http://landvoicelearning.com/#21906 taking 5 50 mg tramadol - rimadyl or tramadol for dogs

Anonymous said...

buy tramadol tramadol hcl no prescription - buy cheap tramadol online

Anonymous said...

learn how to buy tramdadol tramadol overnight - tramadol for dogs online

Anonymous said...

learn how to buy tramdadol tramadol online from usa - tramadol online florida

Anonymous said...

http://landvoicelearning.com/#62431 buy tramadol cod free - tramadol withdrawal paws

Anonymous said...

buy tramadol tablets tramadol 50 mg kaufen - buy tramadol online florida

Anonymous said...

http://buytramadolonlinecool.com/#30694 buy tramadol online with american express - buy tramadol online overnight fedex

Anonymous said...

ways to buy ativan online cheap ativan online - ativan for nausea

Anonymous said...

tramadol overnight shipping tramadol dosage 10 lb dog - can you buy tramadol in greece

Anonymous said...

buy tramadol buy discount tramadol - tramadol withdrawal medication

Anonymous said...

ways to buy ativan online ativan getting high - 8 ativan

Anonymous said...

cheap ativan ativan overdose coma - effects of ativan overdose

Anonymous said...

ways to buy ativan online ativan dosage in elderly - ativan side effects on children

Anonymous said...

buy ativan online ativan dosage for the elderly - where to buy ativan online

Anonymous said...

lorazepam online buy ativan online pharmacy - effects of 1mg lorazepam

Anonymous said...

http://staam.org/#36750 tramadol withdrawal vomiting - buy tramadol online no prescription

Anonymous said...

buy tramadol online tramadol online international - tramadol purchase usa

Anonymous said...

http://staam.org/#90873 generic tramadol 50 mg - tramadol 50 mg para que es

Anonymous said...

buy xanax online valium vs xanax recreational use - buy xanax online mexico

Anonymous said...

buy xanax online xanax withdrawal after one month - xanax valium overdose

Anonymous said...

http://staam.org/#50589 tramadol risks - tramadol 50 mg safe

Anonymous said...

http://ranchodelastortugas.com/#64581 xanax for anxiety - xanax for separation anxiety in dogs

Anonymous said...

buy tramadol online tramadol dosage and side effects - tramadol for dogs and humans the same

Anonymous said...

buy xanax online qualitest 2mg xanax - there generic xanax

Anonymous said...

http://ranchodelastortugas.com/#72895 xanax high forum - xanax dosage and alcohol

Anonymous said...

buy tramadol online tramadol for dogs dosage chart - tramadol 50 mg normal dosage

Anonymous said...

buy tramadol overnight shipping buy tramadol valium online - illegal buy tramadol online usa

Anonymous said...

buy tramadol online ultram vs tramadol generic - tramadol for dogs dosage

Anonymous said...

buy tramadol online tramadol hcl 50 mg ingredients - tramadol 50 mg bluelight

Anonymous said...

buy tramadol online tramadol for dogs medication - tramadol withdrawal nausea

Anonymous said...

http://staam.org/#50589 where to buy tramadol online safely - tramadol ultram wiki

Anonymous said...

buy xanax cheap xanax dosage in children - long do effects 2mg xanax last

Anonymous said...

buy xanax online buy xanax no prescription - xanax high yahoo

Anonymous said...

http://ranchodelastortugas.com/#30416 xanax overdose many mg - blue xanax pills generic

Anonymous said...

http://ranchodelastortugas.com/#37284 signs of drug abuse xanax - 6 xanax and alcohol

Anonymous said...

buy xanax online without prescription drug interactions with xanax and vicodin - xanax generic manufacturers

Anonymous said...

viagra viagra prezzo italia viagra

Anonymous said...

cytotec to buy - order misoprostol cytotec - buy cytotec online malaysia

Anonymous said...

buy tramadol onlinereviews buy tramadol usa - buy tramadol cod next day delivery

Anonymous said...

cialis generico buy cialis in the australia cialis achat cialis

Anonymous said...

order ambien from canada buy ambien mastercard - buy ambien 10mg online

Anonymous said...


http://bit.ly/11E6sdj With 24/7 call and claim centers, no matter when you need them, this company is there. Several insurance providers will offer "well being advantages" such as either primary savings or high quality reductions. They will list several different companies and the discounts they offer.

xumeiqing said...

151212meiqing
canada goose jackets
louis vuitton outlet
christian louboutin outlet
ugg boots
north face jackets
uggs on sale
uggs for sale
hollister uk
michael kors uk
timberland boots
ugg outlet
louis vuitton handbags
burberry outlet
oakley sunglasses
nike air max
michael kors outlet
polo ralph lauren
ugg outlet
true religion jeans
cheap jordans
marc jacobs
ugg australia
hollister kids
louis vuitton
christian louboutin outlet
nike air max
louis vuitton handbags
gucci handbags
coach outlet store online
oakley sunglasses
hollister clothing
ray ban outlet
michael kors outlet online
canada goose jackets
prada outlet
ray ban sunglasses outlet
jordan retro
ray-ban sunglasses
prada uk
louis vuitton outlet stores

Qing Cai said...

lacoste polo shirts
swarovski outlet
michael kors uk
kobe bryants shoes
mcm outlet
toms outlet
rolex watches
mulberry outlet
louis vuitton neverfull
michael kors canada
michael kors outlet online
longchamp handbags
nike air huarache
longchamp outlet
polo ralph lauren
snapbacks wholesale
oakley sunglasses
soccer jerseys
polo shirts
swarovski crystal
ralph lauren outlet
hermes birkin bag
true religion jeans
hermes outlet
ray ban sunglasses
herve leger dresses
juicy couture outlet
toms shoes
swarovski jewelry
lebron james shoes
cai20160422

Nicky Paul said...

A phone call is a phone bring in which somebody converses with a few individuals in the meantime. The phone calls might be intended to permit the called gathering to partake amid the call, or the ring might be set so that the called party simply listens into the call and can't talk. It is some of the time called ATC (sound video chat).
Telephone Conference Call

Yuanyuan Lin said...

7.12lllllyuan"oakley sunglasses wholesale"
"louis vuitton handbags outlet"
"ray-ban sunglasses"
"michael kors wallet"
"ray ban sunglasses"
"oakley sunglasses wholesale"
"longchamp handbags"
"juicy couture tracksuit"
"prada sunglasses for women"
"swarovski outlet"
"ralph lauren polo"
"longchamp outlet online"
"links of london"
"cartier watches"
"nike roshe run"
"asics"
"ferragamo outlet"
"cheap ray ban sunglasses"
"polo ralph lauren"
"michael kors outlet"
"nike tn pas cher"
"ray ban sunglasses"
"tiffany and co"
"tory burch outlet online"
"michael kors outlet"
"mulberry outlet"
"tiffany jewellery"
"tory burch outlet"
"babyliss pro"
"police sunglasses for men"
"prada outlet online"
"soccer jerseys"
"michael kors clearance"
"mcm outlet"
"true religion jeans outlet"
7.12

Leo Linnh said...

head soccer 2
head soccer
head soccer unblocked
Big Head Soccer
Big Head Football
big head basketball

big head basketball
Head Soccer
Big Head Soccer
Big Head Football

Big Head Football
big head basketball
Big Head Soccer
Head Soccer

big head basketball
Head Soccer
Big Head Soccer
Big Head Football

cara menggugurkan kandungan said...

for beginners like me need a lot of reading and searching for information on various blogs. and articles that you share a very nice and inspires me

chenmeinv0 said...

michael kors handbags
polo outlet
timberland outlet
le coq sportif shoes
oakley sunglasses
nike roshe
air jordan pas cher
michael kors handbags
ray ban sunglasses
michael kors
hzx20170225

kenzo julian said...

This is a very good article material and it is very useful for us all. thank you . cara menggugurkan kandungan

John said...

longchamp bags
christian louboutin shoes
discount oakley sunglasses
louboutin outlet
cheap air max
ralph lauren
true religion outlet online
michael kors outlet canada
longchamp handbags
yeezy boost
20170703yuanyuan

dong dong23 said...

uggs classic boots
nike store
ugg outlet
ugg boots
nike factory outlet
fitflops sale clearance
coach outlet
mac cosmetics
vibram fivefingers
louis vuitton
20179.25wengdongdong

林磊 said...

2017106 leilei3915

ugg boots
cheap jerseys
kate spade handbags
michael kors outlet online
michael kors handbags clearance
ralph lauren shirts
coach outlet online
prada outlet
coach outlet online
fred perry polo

haiyan wu said...

yeezy 700
michael kors handbags
true religion jeans
cheap nfl jerseys
golden goose francy
adidas yeezy
yeezy shoes
yeezy boost 350
kayno
fitflops sale clearance

love said...

zzzzz2018.7.24
jordan shoes
ralph lauren outlet
supreme shirt
ralph lauren outlet
christian louboutin outlet
pandora
jimmy choo shoes
ray ban eyeglasses
louboutin shoes
canada goose outlet

xjd7410@gmail.com said...

20180727 junda
adidas ultra boost
grizzlies jerseys
michael kors outlet
nets jerseys
michael kors outlet
air jordan 4
ralph lauren polo
ray ban outlet
coach outlet
air jordan 4

Misty Moon said...

I really enjoyed your blog Thanks for sharing such an informative post.
clipping path
clipping path service
background removal
car editing