I need to adapt an old XSLT file that transforms an XML file into HTML/JS. It doesn't work anymore.
Here is the original JS code generated in XSLT file :
function processVisibility(item, visible) {
var childs = item.childNodes;
for (var i = 0; i < childs.length; i++) {
if ( (childs.item(i).className == "ArchiveObject")
|| (childs.item(i).className == "Document")
|| (childs.item(i).className == "Attachment") ) {
if (visible) {
childs.item(i).style.display = "block";
} else {
childs.item(i).style.display = "none";
}
}
}
}
The string "< ;" is not transformed into < in the output HTML. As a result, JS code doesn't work in modern browsers.
If I replace "< ;" with < in the XSLT file, I get an error and that's normal. So I tried creating a variable:
<xsl:variable name="chevron"><</xsl:variable>
and
function processVisibility(item, visible) {
var childs = item.childNodes;
for (var i = 0; i <xsl:value-of select="$chevron" disable-output-escaping="yes"/> childs.length; i++) {
if ( (childs.item(i).className == "ArchiveObject")
|| (childs.item(i).className == "Document")
|| (childs.item(i).className == "Attachment") ) {
if (visible) {
childs.item(i).style.display = "block";
} else {
childs.item(i).style.display = "none";
}
}
}
}
But the HTML output does not contain < but "< ;" How can I get < to be generated in HTML using XSLT?
I use the HTML serialization method but it doesn't work. Here is the beginning of the XSLT file:
<xsl:stylesheet version="1.0"
xmlns:sada="sada:v1.0"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:ccts="urn:un:unece:uncefact:documentation:standard:CoreComponentsTechnicalSpecification:2"
exclude-result-prefixes="sada xsl xsd ccts">
<xsl:output method="html" indent="yes" media-type="text/html" encoding="UTF-8"/>
<xsl:template match="/">
<html>
<head>
<script type="text/javascript">
function processVisibility(item, visible) {
var childs = item.childNodes;
for (var i=0; i<childs.length; i++) {
if ( (childs.item(i).className == "ArchiveObject")
|| (childs.item(i).className == "Document")
|| (childs.item(i).className == "Attachment") ) {
if (visible) {
childs.item(i).style.display = "block";
} else {
childs.item(i).style.display = "none";
}
}
}
} </script>
<title>Standard (SADA)</title>
</head>
<body>
<xsl:apply-templates />
</body>
</html>
</xsl:template>
</xsl:stylesheet>```
and un exemple of xml source :
```<?xml version="1.0" encoding="UTF-8"?>
<ArchiveTransfer xmlns="sada:v1.0" xmlns:ns2="http://www.w3.org/2000/09/xmldsig#" xmlns:premis="info:lc/xmlns/premis-v2" xmlns:ns4="http://www.w3.org/1999/xlink" xmlns:ns5="http://www.w3.org/2001/XMLSchema-instance" Id="_20240314211030158" ns5:schemaLocation="sada:v1.0 sada_v1-0_archivetransfer.xsd info:lc/xmlns/premis-v2 http://www.loc.gov/premis/v2/premis.xsd">
<Date>2024-05-01T22:37:26.047+02:00</Date>
<TransferIdentifier/>
</ArchiveTransfer>```
<xsl:text disable-output-escaping="yes"><</xsl:text><xsl:output method="html" indent="yes" media-type="text/html" encoding="UTF-8"/>. And tried adding:doctype-system="about:legacy-doctype". But this does not change the character. I provide a more complete example of the XSLT file below.scriptelement is in a namespace, to havexsl:output method="html"apply to ascriptelement to ensure its content is treated as CDATA you need to ensure the script element is no namespace. Or use external script.