Whimusical
Whimusical

Reputation: 6649

xQuery variable holding wildcard * (any)

I would like to parametrize a XPATH filter for a function, but would like to have the chance of indicating I want all the results. Clear example

declare function search-by-author($author as xs:string*) as element()*{
   /Data/Books[@autor=$author]
};

search-by-author(()) -> If null no results even if some books did not have @autor attr
search-by-author('') -> If empty, only shows those books with @author=''
search-by-author(('a','b')) -> Returns books by authors 'a' or 'b'
search-by-author(*) -> THIS MY CURRENT PROBLEM. 

How could I emulate this? Is there any special param or syntax to pass * as a variable, so I can do

 $var = * --> /Data/Books[@autor=$var] 

or

 $var = EMPTY --> /Data/Books[@autor=$var] --> /Data/Books[@autor]

Thank you!

Upvotes: 1

Views: 3259

Answers (1)

Michael Kay
Michael Kay

Reputation: 163595

With

declare function search-by-author($author as xs:string*) as element()*{
   /Data/Books[$author='*' or @autor=$author]
};

you can do

search-by-author('*')

Upvotes: 1

Related Questions