Home > database >  Meaning of xslt match expression
Meaning of xslt match expression

Time:01-20

I am migrating a xslt to php and I came across the following match expression:

string(node/text()[not(. < node/text())]) = '8'

The part that confuses me is the [not(. < node/text())] condition.

CodePudding user response:

The [not(. < node/text())] condition in the match expression is a way to select only the text node that has the maximum value among all the text nodes of the node element.

Here's what each part of the condition does:

  1. node/text() selects all the text nodes that are children of the node element.
  2. . refers to the current text node being processed.
  3. < is the less-than operator, which compares the values of two nodes.
  4. not(...) negates the condition inside the parentheses, so it will select all text nodes for which the condition inside the parentheses is false.

So, the complete condition selects all text nodes that are children of the node element and have a value that is greater than or equal to all other text nodes under the node element.

Then, the string(...) = '8' part of the match expression compares the value of the selected text node (i.e. the text node that has the maximum value among all the text nodes of the node element) to the string '8'. If they match, the match expression returns true, otherwise it returns false.

In this way, the match expression is going to select the maximum value among all the text nodes of the node element and check if it is equal to 8.

CodePudding user response:

I believe the answer given above by Akzy correctly assesses the intent of the match pattern: to find the text node that has the maximum value among all its siblings (i.e. all text nodes that are children of the same node element) and compare it to the string "8"; if both tests are satisfied, then there is a match.

However, there are two problems that will prevent the given pattern from achieving such intent:

  1. A match expression cannot contain an unescaped < character;
  2. The expression does not in fact compare each text node to its siblings; it compares it to text nodes that are children of its child node element - which of course cannot exist, because a text node has no children. Therefore any text node will satisfy the provided predicate [not(. < node/text())] - as can be seen in an arbitrary example here: https://xsltfiddle.liberty-development.net/jyygmE5

In order to fulfill its presumed intention, the match pattern needs to be corrected to:

string(node/text()[not(. &lt; ../../node/text())]) = '8'

although the purpose of converting both sides to string remain unclear.

CodePudding user response:

Firstly, there is no such thing as a "match expression". There are two closely related things, XPath expressions and match patterns. This is a valid XPath expression, it is not a valid match pattern.

string(node/text()[not(. < node/text())]) = '8'

And, as others have pointed out, its value is always false.

Looks as if the person who wrote the code was as confused as you are.

  • Related