FontoXML / fontoxpath

A minimalistic XPath 3.1 implementation in pure JavaScript
MIT License
133 stars 17 forks source link

`XPTY0004: Multiplicity of function argument of type xs:string? for starts-with is incorrect. Expected "?", but got "+".` error when passing `.//text()` to function that takes a string #645

Closed DetachHead closed 3 months ago

DetachHead commented 3 months ago

the following xpath works in both chrome and firefox's xpath 1 implementation, but fails in fontoxpath:

starts-with(.//text(), 'asdf')
Error: XPTY0004: Multiplicity of function argument of type xs:string? for starts-with is incorrect. Expected "?", but got "+".
  at <functionCallExpr>:1:1 - 1:31

playground

DrRataplan commented 3 months ago

Hey there,

Good question! Atomization is responsible for this behaviour: https://www.w3.org/TR/xpath-31/#id-atomization.

Step by step, this happens:

  1. The function is resolved and we see we want xs:string? as the first argument (https://www.w3.org/TR/xpath-functions-31/#func-starts-with)
  2. We start evaluating the argument, we see we have two or more nodes
  3. We start atomizing those nodes, we get at least two strings
  4. We see this cannot fit into the xs:string? argument, which accepts sequences with length one or zero.
  5. We throw the corresponding error.

This is described in the spec here: https://www.w3.org/TR/xpath-31/#dt-function-conversion.

You will need to string-join here, or to reproduce the XPath 1.0 compatibility mode: starts-with(//text => head(), 'abc')

In XPath 1.0 this works, like you observed. Technically you can reproduce this behaviour in XPath 3.1 with the use of the XPath 1.0 compatibility mode. This is not implemented and to be honest, I have no plans implementing this...

Kind regards,

Martin

DetachHead commented 3 months ago

thanks for the explanation

DrRataplan commented 3 months ago

No problem, my pleasure!

On Thu, 1 Aug 2024 at 08:42, DetachHead @.***> wrote:

thanks for the explanation

— Reply to this email directly, view it on GitHub https://github.com/FontoXML/fontoxpath/issues/645#issuecomment-2262171832, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABGKEJCXRU6XOURKWOKIELTZPHKE3AVCNFSM6AAAAABLZQM7HCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDENRSGE3TCOBTGI . You are receiving this because you commented.Message ID: @.***>