我无法将 XML 文件转换为 HTML。它不会将任何 XML 数据解析为 HTML 文件,仅写入表头。我是 XML 新手,那些命名空间和 URI 让我很困惑。我认为进口有问题。 这是我的 XML 文件:
<?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>
这是 XSL 文件(两者都在同一文件夹中):
<?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>
这是我的 Java 代码:
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());
}
}
}
使用方法:
public class Main {
public static void main(String[] args){
BanksXMLTransformer.transform("banks.xml","banks.xsl","banks.html");
}
}
我的 HTML 文件如下所示:
<?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>
最佳答案
I'm new to XML and those namespaces and URI's confuse me allot.
好吧,您已经声明了 namespace ,但您没有使用它。此外,您没有正确指向 myspace:deposit
的子节点。试试这个方法:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:myspace="http://bankinfo.com/banks"
exclude-result-prefixes="myspace">
<xsl:template match="/">
<html>
<body>
<table>
<tr>
<th>name</th>
<th>country</th>
<th>type</th>
<th>accountId</th>
<th>depositor</th>
<th>amount</th>
<th>profitability</th>
<th>constraints</th>
</tr>
<xsl:for-each select="myspace:banks/myspace:bank">
<tr>
<td><xsl:value-of select="myspace:name"/></td>
<td><xsl:value-of select="myspace:country"/></td>
<td><xsl:value-of select="myspace:deposit/@type"/></td>
<td><xsl:value-of select="myspace:deposit/@accountId"/></td>
<td><xsl:value-of select="myspace:deposit/myspace:depositor"/></td>
<td><xsl:value-of select="myspace:deposit/myspace:amount"/></td>
<td><xsl:value-of select="myspace:deposit/myspace:profitability"/></td>
<td><xsl:value-of select="myspace:deposit/myspace:constraints"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
或者,也许,更有效一点:
<xsl:for-each select="myspace:banks/myspace:bank/myspace:deposit">
<tr>
<td><xsl:value-of select="../myspace:name"/></td>
<td><xsl:value-of select="../myspace:country"/></td>
<td><xsl:value-of select="@type"/></td>
<td><xsl:value-of select="@accountId"/></td>
<td><xsl:value-of select="myspace:depositor"/></td>
<td><xsl:value-of select="myspace:amount"/></td>
<td><xsl:value-of select="myspace:profitability"/></td>
<td><xsl:value-of select="myspace:constraints"/></td>
</tr>
</xsl:for-each>
关于java - 如何使用 XSL 和 Java Transformer 类将 XML 转换为 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30822376/