lowlighter / libs

🍱 Collection of carefully crafted TypeScript standalone libraries. Minimal, unbloated, convenient.
https://jsr.io/@libs
MIT License
120 stars 11 forks source link

XSLT throws multiple prolog error #21

Closed oliverjam closed 1 year ago

oliverjam commented 1 year ago

As far as I can tell this XML using XSLT is valid:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet href="/rss.xsl" type="text/xsl"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>oli's rss feed</title>
  <link href="https://oliverjam.es/feed.xml" rel="self"/>
  <link href="https://oliverjam.es"/>
  <updated>2023-01-17T00:00:00.000Z</updated>
  <id>https://oliverjam.es</id>
  <author>
    <name>oli</name>
  </author>
</feed>

but parsing it throws with SyntaxError: Multiple prolog declaration found. Removing the <?xml-stylesheet?> fixes the error.

It looks like this is because the prolog token is defined to start with just "<?xml", which matches both.

https://github.com/lowlighter/xml/blob/448b77319701201654c8dc3f5a6ea7451fdf9f90/utils/types.ts#L130

lowlighter commented 1 year ago

Will be supported by #22

Tbh I'm not super familiar with xml-stylesheet and I can't find how this is supposed to be handled within the XML spec (https://www.w3.org/TR/xml/#sec-well-formed), maybe they're supposed to be within the processing instructions (https://www.w3.org/TR/xml/#NT-PI) but looks likes the -xml is a suffix instead in them.

Anyways I tested with the sample you provided and it yields something like this:

{
  xml: { "@version": 1, "@encoding": "utf-8" },
  "$stylesheets": [ { "@href": "/rss.xsl", "@type": "text/xsl" } ],
  feed: {
    "@xmlns": "http://www.w3.org/2005/Atom",
    title: "oli's rss feed",
    link: [
      { "@href": "https://oliverjam.es/feed.xml", "@rel": "self", "#text": null },
      { "@href": "https://oliverjam.es", "#text": null }
    ],
    updated: "2023-01-17T00:00:00.000Z",
    id: "https://oliverjam.es",
    author: { name: "oli" }
  }
}

Lmk if this looks ok, if yes I'll issue a new version with this patch 👍

oliverjam commented 1 year ago

Looks good to me, thank you!

XSLT is kind of a mystery to me as well tbh, no idea how they're supposed to be handled.