I am trying to use xslt to render an xml file generated by a software in use at my work.
There are some CDATA content in the xml. When I transform it the content of the CDATA is displayed as text but I would like it is only not shown.
I found a way to make it empty so that nothing appear while I don't need to exploit it but I have to manage all cases.
My question is : How can I manage all the CDATA content as standard text (accessible with value-of) so that it will not appear while I don't select it for rendering ?
The xml file looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<Domain>
<Properties>
<Property id="DOM00000" key="mykey1" value="value1"/>
<Property id="DOM00001" key="mykey2" value="value2"/>
</Properties>
<Tokens>
<Token name="token1" comment=""><![CDATA[mydata1---blah-blah-blah]]></Token>
<Token name="token2" comment=""><![CDATA[mydata2---blah-blah-blah]]></Token>
</Tokens>
<Resources>
<Resource name="res1" type="W" current="0">
<Value><![CDATA[10]]></Value>
</Resource>
<Resource name="res2" type="W" current="0">
<Value><![CDATA[10]]></Value>
</Resource>
</Resources>
</Domain>
The xsl file I am using is this one :
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/Domain/Properties">
<table border="1">
<tr bgcolor="#9acd32">
<th style="text-align:left">id</th>
<th style="text-align:left">key</th>
<th style="text-align:left">value</th>
</tr>
<xsl:for-each select="/Domain/Properties/Property">
<tr>
<td><xsl:value-of select="@id" /></td>
<td><xsl:value-of select="@key" /></td>
<td><xsl:value-of select="@value" /></td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
<xsl:template match="/Domain/Tokens/Token">
</xsl:template>
</xsl:stylesheet>
Edit Here is what I use :
<form method="POST">
<label for="xmlfileinput">Fichier XML</label><input type="file" name="myxmlfile" id="xmlfileinput" accept=".xml"><br>
<label for="xslfileinput">Fichier XSL</label><input type="file" name="myxslfile" id="xslfileinput" accept=".xsl"><br>
<input type="button" onclick="handleFiles()">
</form>
function handleFiles(e) {
var myxmlfile = document.getElementById("xmlfileinput").files;
var myxslfile = document.getElementById("xslfileinput").files;
var xmlreader = new FileReader();
var xslreader = new FileReader();
xmlreader.onload = function() {
var xml = new DOMParser().parseFromString(xmlreader.result, "text/xml");
xslreader.onload = function() {
var xsl = new DOMParser().parseFromString(xslreader.result, "text/xml");
if (window.ActiveXObject /*|| xhttp.responseType == "msxml-document"*/)
{
ex = xml.transformNode(xsl);
document.getElementById("example").innerHTML = ex;
}
// code for Chrome, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument)
{
xsltProcessor = new XSLTProcessor();
xsltProcessor.importStylesheet(xsl);
resultDocument = xsltProcessor.transformToFragment(xml, document);
while (document.getElementById("example").firstChild) {
document.getElementById("example").removeChild(document.getElementById("example").firstChild);
}
document.getElementById("example").appendChild(resultDocument);
}
};
xslreader.readAsText(myxslfile[0]);
};
xmlreader.readAsText(myxmlfile[0]);
}
In the result with these files the "mydata..." from the Tokens are not shown but the "10" from the Ressources are here.
Thanks