wmsigler 0 Newbie Poster

Is it possible to apply XSLT on XML output from a Perl CGI script?

I.e. I have a registration form, when submitted it makes an Ajax request, building a query string from the form inputs. The request is made to a Perl CGI script.

This script can be called two ways, both using Ajax. The first simply reads an XML file, and outputs the file, and Ajax sticks it in a div container. The other way (via form submission and the query string), actually appends the form data to the XML file, e.g.:

<root>
  <entry>data</entry>
</root>

now becomes:

<root>
  <entry>data</entry>
  <entry>moredata</entry>
</root>

Now, at the top of the XML file I have a link to an XSLT stylesheet. My original intentions were to output the XML file to the div container, but have it apply the XSLT templates so it would be formatted like an HTML table. However, it seems to be ignoring the XSLT completely, simply outputting the raw XML output returned from the CGI program.

Is there a way to fix this, and have it use my XSLT formatting? Much thanks in advance! :)

Dani AI

Generated

For : the browser will not automatically run the <?xml-stylesheet?> processing instruction on XML that was fetched by Ajax and then dumped into a DIV. That behavior is normal — xml-stylesheet is honored when the browser loads the XML as a top-level document, not when the XML is inserted into an existing HTML page. Two reliable approaches follow: transform in the browser (client-side) or transform in Perl (server-side) and return HTML.

Client-side (modern browsers): fetch both XML and XSL, parse them, then run the stylesheet with XSLTProcessor and insert the resulting nodes. This keeps the round-trip small and lets the page update only the target DOM region. See the XSLTProcessor API on MDN for details: XSLTProcessor documentation.

// fetch XML and XSL, transform, insert
Promise.all([
  fetch('/data.xml').then(r => r.text()).then(t => new DOMParser().parseFromString(t, 'application/xml')),
  fetch('/style.xsl').then(r => r.text()).then(t => new DOMParser().parseFromString(t, 'application/xml'))
]).then(([xmlDoc, xslDoc]) => {
  const proc = new XSLTProcessor();
  proc.importStylesheet(xslDoc);
  const frag = proc.transformToFragment(xmlDoc, document);
  const target = document.getElementById('result');
  target.textContent = ''; // clear
  target.appendChild(frag);
});

Server-side (Perl): transform to HTML before returning. Use XML::LibXML + XML::LibXSLT to apply the stylesheet and then send Content-Type: text/html. This avoids browser XSLT quirks and lets the server use more powerful processors (or XSLT 2.0/XQuery via Saxon if needed). See XML::LibXSLT on CPAN: XML::LibXSLT.

Notes: ensure same-origin or proper CORS headers for the XSL file, set correct response Content-Type, and validate the XSL if the transform silently fails. If XSLT 2.0 features are required, perform server-side transforms with a processor that supports 2.0 (for example Saxon).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.