I can't transform my XML file into HTML. It doesn't parse any XML data into HTML file, in only writes table headers. I'm new to XML and those namespaces and URI's confuse me allot. I think it is something wrong with imports. Here is my XML file:
<?xml version="1.0" encoding="UTF-8"?>
<banks xmlns="http://bankinfo.com/banks"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://bankinfo.com/banks banks.xsd">
<bank>
<name>HKB Bank</name>
<country>Bavaria</country>
<deposit type="demand deposit" accountId="d172061">
<depositor>Alissa Lange</depositor>
<amount>5000</amount>
<profitability>7.0</profitability>
<constraints>P10M</constraints>
</deposit>
</bank>
</banks>
Here is XSL file(both are in same folder):
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:myspace="http://bankinfo.com/banks"
version="3.0">
<xsl:template match="/">
<html>
<body>
<table>
<th>
<td>name</td>
<td>country</td>
<td>type</td>
<td>accountId</td>
<td>depositor</td>
<td>amount</td>
<td>profitability</td>
<td>constraints</td>
</th>
<xsl:for-each select="banks/bank">
<tr>
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="country"/></td>
<td><xsl:value-of select="@type"/></td>
<td><xsl:value-of select="@accountId"/></td>
<td><xsl:value-of select="depositor"/></td>
<td><xsl:value-of select="amount"/></td>
<td><xsl:value-of select="profitability"/></td>
<td><xsl:value-of select="constraints"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
And here is my Java Code:
public class BanksXMLTransformer {
public static void transform(String xmlFile, String xslFile, String targetFile) {
TransformerFactory factory = TransformerFactory.newInstance();
try {
Transformer transformer = factory.newTransformer(new StreamSource(xslFile));
transformer.transform(new StreamSource(xmlFile), new StreamResult(targetFile));
} catch (TransformerConfigurationException e) {
System.err.println(e.getMessage());
} catch (TransformerException e) {
System.err.println(e.getMessage());
}
}
}
Use of method:
public class Main {
public static void main(String[] args){
BanksXMLTransformer.transform("banks.xml","banks.xsl","banks.html");
}
}
My HTML file looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://bankinfo.com/banks">
<body>
<table>
<th>
<td>name</td>
<td>country</td>
<td>type</td>
<td>accountId</td>
<td>depositor</td>
<td>amount</td>
<td>profitability</td>
<td>constraints</td>
</th></table>
</body>
</html>
Well, you have declared the namespace, but you're not using it. In additin, you're not pointing correctly to the nodes that are children of
myspace:deposit
. Try it this way:Or, perhaps, a bit more efficiently: