Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[XPath] Functions that determine if a given sequence starts with another sequence or ends with another sequence #96

Closed
dnovatchev opened this issue Nov 22, 2021 · 3 comments
Labels
Feature A change that introduces a new feature XQFO An issue related to Functions and Operators

Comments

@dnovatchev
Copy link
Contributor

dnovatchev commented Nov 22, 2021

It is surprising that we are at version 4 and still are missing:

(1) fn:starts-with-sequence($container as item()*, $maybe-start as item()*, 
                            $compare as function(item(), item()) as xs:boolean := deep-equal)
                            ) as xs:boolean

and

(2) fn:ends-with-sequence($container as item()*, $maybe-end as item()*, 
                          $compare as function(item(), item()) as xs:boolean := deep-equal)
                          ) as xs:boolean

(2) above is a shorthand for:

fn:starts-with-sequence(reverse($container), reverse($maybe-end)) 

Examples:

fn:starts-with-sequence(('a', 'b', 'c', 'd'), ('a', 'b')) returns true()

fn:starts-with-sequence(('a', 'b', 'c', 'd'), ('a', 'c')) returns false()

fn:ends-with-sequence(('a', 'b', 'c', 'd'), ('c', 'd')) returns true()

fn:ends-with-sequence(('a', 'b', 'c', 'd'), ('b', 'd')) returns false()

('a', 'b', 'c', 'd') => starts-with-sequence(('a', 'b')) returns true()

('a', 'b', 'c', 'd') => starts-with-sequence(('a', 'c')) returns false()

('a', 'b', 'c', 'd') => ends-with-sequence(('c', 'd')) returns true()

('a', 'b', 'c', 'd') => ends-with-sequence(('b', 'd')) returns false()

One possible implementation:

let $starts-with-sequence := function($seq1 as item()*, $seq2 as item()*, $self as function(*))
{
   empty($seq2)
  or
   head($seq1) eq head($seq2) and $self(subsequence($seq1, 2), subsequence($seq2, 2), $self)
}
  return
    $starts-with-sequence(('a', 'b', 'c', 'd'), ('a', 'b', 'c'), $starts-with-sequence)
@dnovatchev dnovatchev changed the title Functions that determine if a given sequence starts with another sequence or ends with another sequence [XPath] Functions that determine if a given sequence starts with another sequence or ends with another sequence Nov 22, 2021
@michaelhkay
Copy link
Contributor

Note workarounds such as

all(for-each-pair($A, $B, my:compare#2))

@dnovatchev
Copy link
Contributor Author

dnovatchev commented Aug 11, 2022

Note workarounds such as
all(for-each-pair($A, $B, my:compare#2))

And for ends-with-sequence():

   all(for-each-pair(reverse($A), reverse($B), my:compare#2))

However this is rather challenging to read and understand.

Thanks god, for strings we have fn:starts-with() and fn:ends-with(), so we will not have to write:

all(for-each-pair(string-to-codepoints($A), string-to-codepoints($B), my:compare#2)

and

all(for-each-pair(reverse(string-to-codepoints($A)), reverse(string-to-codepoints($B)), my:compare#2)

Some of us can treat this as a problem, spend some time and find a suitable XPath expression.

The whole meaning of this proposal is that doing this will no longer be a problem for anyone.

This proposal aims at achieving conceptual integrity, simplicity, readability and easy understandability / convenience

@dnovatchev dnovatchev added XQFO An issue related to Functions and Operators Feature A change that introduces a new feature labels Sep 14, 2022
michaelhkay added a commit to michaelhkay/qtspecs that referenced this issue Oct 25, 2022
@michaelhkay
Copy link
Contributor

The issue can now be closed, PR #222 was accepted on 2022-11-01
https://qt4cg.org/meeting/minutes/2022/11-01.html#pr-sequence-comparison

ndw pushed a commit to ndw/qtspecs-xslt4 that referenced this issue Nov 16, 2022
starts-with-sequence, ends-with-sequence, contains-sequence as per issues qt4cg#96 and part of qt4cg#94
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature A change that introduces a new feature XQFO An issue related to Functions and Operators
Projects
None yet
Development

No branches or pull requests

2 participants