user10417774
user10417774

Reputation:

Count() on blank space XQUERY

I want to count how many books I have listed I have:

books = "book-one book-two book-three ..."

If i use

count(books)

it only returns 1 no matter how long my string is, since count separates on , I'm guessing.

How can I count separating on blank-space instead?

Upvotes: 1

Views: 271

Answers (1)

Joe Wicentowski
Joe Wicentowski

Reputation: 5294

Assuming the books are separated by a space, you can split the string into a sequence of strings using the fn:tokenize() function:

fn:tokenize("book-one book-two book-three", " ")

This will return a sequence of the books:

("book-one", "book-two", "book-three")

The fn:tokenize() function can work with literal strings like " ", or it can take regular expressions like "\s+" (to mean "one or more whitespace characters"). This allows for some pretty sophisticated pattern matching.

For the canonical description of this function, see https://www.w3.org/TR/xpath-functions-31/#func-tokenize, and for more on regular expressions in XPath and XQuery, see https://www.w3.org/TR/xpath-functions-31/#string.match.

Upvotes: 2

Related Questions