NULL literal in XPath - xpath

Is there a NULL literal in XPath 1.0 or 2.0?
My use case is that I have a conditional (if then else) XPath expression and I want to return NULL to signify a certain condition. I am afraid that returning an empty string might be ambiguous in my case as it could be a valid result of the other part of the if then else expression.

The empty sequence () can be used as such. It is also returned if there is no result for a path expression.
let $foo := "foo"
return
if ($foo = ("foo", "bar", "batz")) then
$foo
else
()
You can check for an empty sequence using
let $result := ()
return empty($result)
If you pass the result of the first XPath expression to your native code, you should be able to distinguish "NULL" from the empty string by having no results (empty sequence / "NULL") or having a result string (which could be empty).

Related

Comparing to a null string in PL/SQL

I am looking for a way to compare a string to a null string the optimal way.
If I do
IF(str1 <> str2) THEN
dbms_output.put_line('Strings are not equal');
END IF;
This gets ignored if str1 is NULL.
I know I can set a null string to something before comparing to make it work
str1 := NVL(str1, 'Empty');
IF(str1 <> str2) THEN
dbms_output.put_line('Strings are not equal');
END IF;
And that works but I wanted to see if there is a better way to handle null string comparisons
You didn't say whether the string you must compare (str1) may be null; nor what the result of the comparison should be if either string is null.
It very rarely (if ever) makes sense to consider a non-null string equal to null. Sometimes it does make sense to consider two null strings as equal (if you think of them as "empty strings" - idiotically, Oracle doesn't make the distinction, mandated by the SQL Standard, between the SQL concept of null, on the one hand, and the empty string concept on the other hand).
The condition below:
str1 = str2
returns TRUE if both strings are non-null and they are equal; it returns FALSE if both strings are non-null and they are different; and it returns UNKNOWN if at least one string is null.
The LNNVL operator (which takes a condition as its argument) returns FALSE when its argument is TRUE and it returns TRUE when its argument is either FALSE or UNKNOWN. So, the condition
lnnvl(str1 = str2)
returns FALSE when the strings are both non-null and equal, and TRUE in all other cases. If you must consider two null strings as "not equal", then this is the condition you need in your code.
If, on the other hand, you must view two null strings as equal, you can use
decode(str1, str2, 0) is not null
In the case above, DECODE returns 0 if str1 and str2 are both non-null and equal, and also when they are both null. (This is why DECODE is often used in exactly this kind of situation). It returns the default (which I didn't specify, so the function will return the "default" default value, which is NULL) in the remaining cases. DECODE will return non-NULL when str1 and str2 are "the same" - either as equal, non-null strings, or as both null.
Of course, the DECODE condition can also be written as
decode(str1, str2, 0) = 0
or even
decode(str1, str2, 0, 1) = 0
to make it more explicit. Some people (not me!) may prefer it that way.
What if str2 literally is 'Empty'? nvl(str1, 'Empty'); (which is what you mean by DECODE(str1, 'Empty') I assume (decode() needs at least three parameters so your original expression would fail)) will give you a false negative. Just use IS NULL.
IF str1 <> str2
OR str1 IS NULL
OR str2 IS NULL THEN
dbms_output.put_line('Strings are not equal');
END IF;
If either str1 or str2 can be NULL then you can use:
IF str1 <> str2
OR str1 IS NULL AND str2 IS NOT NULL
OR str1 IS NOT NULL AND str2 IS NULL
THEN
dbms_output.put_line('Strings are not equal');
END IF;
db<>fiddle here

Unable to Match numbers only using Regular Expression in golang using Regex

matched, err := regexp.MatchString(`[0-9]`, `a.31`)
fmt.Println(matched)
The above expression is returning true.. Isn't it supposed to be false?
I want to extract only numbers but why is "a.31" considered true? I've noticed that having atleast a number in the string will return "true"...
How to make it return "true" only for numbers?

Wrong preg_match - php

I have a question about regular expressions, I can't understand why the function returns 1 when it should return 0, because the string doesn't match the regular expression.
$t = preg_match("/DOC|doc|doc_[IVXCL]{1,6}_[A-Z]{1}_[A-Z0-9]{2,5}\.pdf/", "DOC_TRF4_DEZ_2014.pdf");
echo $t;
It returns TRUE because it matches DOC.
You have to group some part in your regex:
preg_match("/(?:DOC|doc|doc)_[IVXCL]{1,6}_[A-Z]{1}_[A-Z0-9]{2,5}\.pdf/", "DOC_TRF4_DEZ_2014.pdf");
// here __^ __^
But it isn't very clear what you're trying to match.

What is the counterpart for expression `?value1:value2` in ruby?

I want to know what the counterpart is in Ruby for this kind of expression:
var status=false;
var xx=new Obj(xx,status?"0":"1",status?"2":"3");
I tried the same in Ruby, but it seems that the syntax:
status?"23":nil
does not work.
Method names can end with question marks, so use more spaces:
status ? "23" : nil
Equivalently you could write:
("23" if status)
Put a space between status and ?. Seems that it might get parsed as a method name status?. Also, don't terminate your sentences with semicolons. And don't use var.
x = status ? "0" : "1"

If statement not working - what is wrong with this syntax?

I get an "Expected Identifier" message against the if line. Any ideas why?
if ([inputA.text isEqualToString:#""]) && ([inputB.text <> isEqualToString:#""]) {
c = 1;
}
I'm trying to say it both inputs are empty...
I presume there isn't an easier way to say if the text is null in Obj C++?
An if statement requires that its condition expression be enclosed in parentheses. You have a compound expression. You've used parentheses around the subexpressions of the logical AND operation (&&), but you haven't surrounded the entire expression in parentheses. (The subexpressions don't actually require parentheses in this case, but they don't hurt.)
Next, you have a random <> in the second subexpression. What is that doing in there? In some languages that is the "not equal" operator, but a) it's not an operator in C or Objective-C, b) it wouldn't go inside a message-send expression like that, and c) you claim you were trying to check that both inputs are empty, so I wouldn't expect you to try to negate the test for equality with the empty string.
So, fixing just those problems yields:
if ([inputA.text isEqualToString:#""] && [inputB.text isEqualToString:#""]) {
c = 1;
}
That said, pie's answer is good, too. It also works if either of the inputs has a nil text property, too.
if ([inputA.text length]==0 && [inputB.text length]==0)
{
c = 1;
}

Resources