Selecting text in XPath using an If-Then like construction

If you want an if-then-else construction in XPath to conditionally select text, you can use the following construction:

let text1 := $body//*:element1/text()
let text2 := $body//*:element2/text()
let text3:=
substring($text1, 1, number(string-length($text1) > 0 ) * string-length($text1)),
substring($text2, 1, number(string-length($text1) = 0 and string-length($text2) > 0) * string-length($text2))

In this example, if text1 is not empty, it will be assigned to text2, otherwise, text2 will be assigned to text3

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s