johnlumley / jwiXML

An iXML processor for JavaScript and SaxonJS
MIT License
13 stars 0 forks source link

Can the jwL:compileGrammar be improved to convert its input to a string if an input node with a grammar is passed in? #2

Open martin-honnen opened 2 years ago

martin-honnen commented 2 years ago

Hi John,

I have started to try to integrate your Invisible XML library in my XSLT 3, SaxonJS based fidde at https://martin-honnen.github.io/xslt3fiddle/index-iXML.html. I hope I have understood the documentation and comments in the code fine, I have run into one oddity where I wonder whether your function signature or the type checking in the function can be improved.

Sometimes I expect the input XML of the XSLT transformation to contain a grammar to be parsed in some XML element e.g. in a sample I have

<?xml version="1.0" encoding="utf-8"?>
<html lang="en">
  <head>
    <title>Saxon-JS 2.4 XSLT 3 and iXML fiddle</title>
  </head>
  <body>
    <section>
      <h1>jwiXML Test</h1>
      <section>
        <h3>Invisible XML example</h3>
        <section>
          <h4>Grammar</h4>
          <pre><code id="grammar1">Countries = Country*.
Country = Name, -'/', Code, -'/', Line, #A?.
Name = ['A'-'Z'],['A'-'Z'].
Code = ['0'-'9'],['0'-'9'],['0'-'9'].
Line = ['0'-'9'],['0'-'9'].</code>
          </pre>
          <h4>Sample data</h4>
          <pre><code id="data">UK/006/10
US/004/12</code></pre>
        </section>
      </section>
    </section>
  </body>
</html>

and then it "only" works fine to pass the string value of that code id="grammar1" element to the jwL:compileGrammar function, i.e. doing

    <xsl:sequence
      select="jwL:parse(jwL:compileGrammar(//code[@id = 'grammar1']/string()), //code[@id = 'data'])?tree"/>

works fine, however simply passing the element node, as in

   <xsl:sequence
      select="jwL:parse(jwL:compileGrammar(//code[@id = 'grammar1']), //code[@id = 'data'])?tree"/>

gives me an error Failed to call method function(a){a instanceof Document?(a=Cb(a),a.compile()):(a=(new Db).parse(a),a.compile());return a}.

Is that expected? I am afraid I am not able to infer what the minimized Cb(a) or (new Db).parse(a) are supposed to do or what kind of input they take.

So I need your help to tell me whether the explicit string conversion I needed to do is a wanted requirement or whether you can improve the function/API to do that automagically.

Full examples are: failure , success.

johnlumley commented 2 years ago

Martin, I’m currently on holiday but should be able to look at your question about Wednesday or Thursday this week

John

Sent from my iPad

On 25 Jul 2022, at 10:58, martin-honnen @.***> wrote:

 Hi John,

I have started to try to integrate your Invisible XML library in my XSLT 3, SaxonJS based fidde at https://martin-honnen.github.io/xslt3fiddle/index-iXML.html. I hope I have understood the documentation and comments in the code fine, I have run into one oddity where I wonder whether your function signature or the type checking in the function can be improved.

Sometimes I expect the input XML of the XSLT transformation to contain a grammar to be parsed in some XML element e.g. in a sample I have

<?xml version="1.0" encoding="utf-8"?>

Saxon-JS 2.4 XSLT 3 and iXML fiddle

jwiXML Test

Invisible XML example

Grammar

Countries = Country*.
Country = Name, -'/', Code, -'/', Line, #A?.
Name = ['A'-'Z'],['A'-'Z'].
Code = ['0'-'9'],['0'-'9'],['0'-'9'].
Line = ['0'-'9'],['0'-'9'].
          

Sample data

UK/006/10
US/004/12

and then it "only" works fine to pass the string value of that code id="grammar1" element to the jwL:compileGrammar function, i.e. doing

<xsl:sequence
  ***@***.*** = 'grammar1']/string()), ***@***.*** = 'data'])?tree"/>

works fine, however simply passing the element node, as in

<xsl:sequence @. = 'grammar1']), @. = 'data'])?tree"/> gives me an error Failed to call method function(a){a instanceof Document?(a=Cb(a),a.compile()):(a=(new Db).parse(a),a.compile());return a}.

Is that expected? I am afraid I am not able to infer what the minimized Cb(a) or (new Db).parse(a) are supposed to do or what kind of input they take.

So I need your help to tell me whether the explicit string conversion I needed to do is a wanted requirement or whether you can improve the function/API to do that automagically.

Full examples are: failure , success.

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you are subscribed to this thread.

martin-honnen commented 2 years ago

Thanks, @johnlumle, with your update the test now works fine.