如何使用带有 Java 转换器 class 的 XSL 将 XML 转换为 HTML

How can I transform XML to HTML using XSL with Java Transformer class

我无法将 XML 文件转换为 HTML。它不会将任何 XML 数据解析到 HTML 文件中,只会写入 table headers。我是 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.

好吧,您已经声明了命名空间,但是您没有使用它。在 additin 中,您没有正确指向 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>