Project

Profile

Help

TypeError: B.replace is not a function

Added by Takatomo Inoue 5 months ago

I use Saxon-JS (2.4) running on the client-side, and I debugged with Safari. And then, I faced the following error:

TypeError: B.replace is not a function. (In 'B.replace(/\r\n|\r(?!\n)/g,"\n")', 'B.replace' is undefined)

According to the stack trace, the error happens at 4589 of SaxonJS2.js, which is:

parseXmlFromString:function(B,n,g){B=B.replace(/\r\n|\r(?!\n)/g,"\n");g||(g="application/xml");try{if("application/xml"===g&&/^.+<\?xml/i.test(B))throw Error();

Judging from that, it does not seem to occur due to my XSLT (SEF) and the input XML.

I wonder if it is a bug or my fault or a defect in my js code.

I would be happy to provide more details if anyone wish to reply.

Thank you!


Replies (7)

Please register to reply

TypeError: B.replace is not a function - Added by Norm Tovey-Walsh 5 months ago

Saxonica Developer Community writes:

I wonder if it is a bug or my fault or a defect in my js code.

That’s likely to be a bug, but it’s hard to tell from the message alone.
If you can provide a small example that we can use to reproduce the
error, that would be ideal.

Be seeing you,
norm

--
Norm Tovey-Walsh
Saxonica

RE: TypeError: B.replace is not a function - Added by Takatomo Inoue 5 months ago

Thank you for your response!

I created a simple project that can reproduce the error. It actually runs on the CDN version of Vue.js

I apply an XSLT to Saxon at lines 34-39 of index.html. Also, on lines 42-44, which are commented, I coded the case when the standard XSLTProcessor of Javascript is used. This runs properly.

Please find the attached zip file.

Takatomo

RE: TypeError: B.replace is not a function - Added by Martin Honnen 4 months ago

Have you checked whether the same error occurs with Chrome and/or Firefox also? As a workaround, you might want to parse the XML source yourself with e.g. new DOMParser().parseFromString(sourceXmlVariable, 'application/xml'), it seems at least Safari doesn't like SaxonJS's attempt to do some sanity check on the XML regarding the XML declaration.

RE: TypeError: B.replace is not a function - Added by Martin Honnen 4 months ago

On further look, it seems you just pass the wrong type of argument to Saxon, you want to use

          const responseXML = await fetch('sample0.xml')
          const xml = await responseXML.text()
          const responseXSL = await fetch('sample0.xsl')
          const xsl = new DOMParser().parseFromString(await responseXSL.text(), 'text/xml')

          /* Using Saxon-JS2 */
          var options = {
            stylesheetLocation: 'sample0.sef.json',
            sourceText: xml,
            destination: "document"
          }

or

          const responseXML = await fetch('sample0.xml')
          const xml = new DOMParser().parseFromString(await responseXML.text(), 'text/xml')
          const responseXSL = await fetch('sample0.xsl')
          const xsl = new DOMParser().parseFromString(await responseXSL.text(), 'text/xml')

          /* Using Saxon-JS2 */
          var options = {
            stylesheetLocation: 'sample0.sef.json',
            sourceNode: xml,
            destination: "document"
          }

RE: TypeError: B.replace is not a function - Added by Takatomo Inoue 4 months ago

Thank you for your reply, Martin!

Yeah, thanks to your comment, the error was gone by changing the code in either way. However, in both of the cases, the return value of SaxonJS.transform(options) is undefined. It does not seem to be processed properly.

The following is the result of console.log(SaxonJS.transform(options)) in Firefox:

Thank you again! Takatomo

RE: TypeError: B.replace is not a function - Added by Martin Honnen 4 months ago

As https://www.saxonica.com/saxon-js/documentation2/index.html#!api/transform/results documents, you get an object having various properties, so the result, in your case, as you don't use xsl:result-document, as the main property you want to use is principalResult, that is a DOM document fragment you can insert with e.g. appendChild to some DOM element in your HTML document e.g. document.body.appendChild(SaxonJS.transform(options).principalResult) would append the children of the result fragment to the HTML body element. I don't remember whether that makes sense for the sample XSLT you had but that should give you a start.

Depending on how far you want to buy into SaxonJS's interactive XSLT approach, you can of course read up on https://www.saxonica.com/saxon-js/documentation2/index.html#!starting/installing, https://www.saxonica.com/saxon-js/documentation2/index.html#!browser/html-page, https://www.saxonica.com/saxon-js/documentation2/index.html#!browser/result-documents and https://www.saxonica.com/saxon-js/documentation2/index.html#!browser/system-events and just let the XSLT create the HTML that SaxonJS automatically inserts into your target HTML document, without the need to handle the result in JavaScript.

RE: TypeError: B.replace is not a function - Added by Takatomo Inoue 4 months ago

Thank you again, Martin!

Actually, I wanted a converted HTML text (not a DOM), so when I set destination: "serialized" instead of document and used principalResult, I managed to get what I wanted. I should have read the documentation carefully.

Thanks for your kind response!

Takatomo

    (1-7/7)

    Please register to reply