Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking all values in the element are same

Tags:

xslt

I have an xml like, values can be

<n1>value1</n1>
<n1>value1</n1>
<n1>value2</n1>

I need to check if all these values are same and if same I would need to assign it to another element. I am using XSLT v1.0.

Thanks,

like image 856
Arun Avatar asked Dec 14 '11 17:12

Arun


People also ask

How do you check if all the values in a list are equal?

You can convert the list to a set. A set cannot have duplicates. So if all the elements in the original list are identical, the set will have just one element. if len(set(input_list)) == 1: # input_list has all identical elements.

How do you check if all the elements in list are same in Java?

allMatch() method. The allMatch() method returns true if all elements of the stream matches with the given predicate. It can be used as follows to check if all elements in a list are the same.

How do I check if all elements are the same in a NumPy array?

By using Python NumPy np. array_equal() function or == (equal operator) you check if two arrays have the same shape and elements. These return True if it has the same shape and elements, False otherwise.


1 Answers

Good question, +1.

Just use:

not(/*/n1[1] != /*/n1)

Assuming that all n1 elements are selected in a variable named $v, this can be expressed in just 14 characters-long XPath expression:

not($v[1] != $v)

Explanation 1:

By definition:

/*/n1[1] != /*/n1

is true() exactly if there exists a node in /*/n1 whose string value isn't equal to the string value of /*/n1[1]

The logical negation of this:

not(/*/n1[1] != /*/n1)

is true() iff no node in /*/n1 exists whose string value isn't equal to the string value of /*/n1[1] -- that is, if all nodes in /*/n1 have the same sting value.

Explanation 2:

This follows from a more general double negation law :

every x has property y

is equivalent to:

There is no x that doesn't have property y
like image 142
Dimitre Novatchev Avatar answered Oct 21 '22 12:10

Dimitre Novatchev