0

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>

1 Answer 1

1

I'm new to XML and those namespaces and URI's confuse me allot.

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:

<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>

Or, perhaps, a bit more efficiently:

<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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.