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


i can't transform xml file html. doesn't parse xml data html file, in writes table headers. i'm new xml , namespaces , uri's confuse me allot. think wrong imports. here 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 xsl file(both 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 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 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> 

i'm new xml , namespaces , uri's confuse me allot.

well, have declared namespace, you're not using it. in additin, you're not pointing correctly nodes children of myspace:deposit. try 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, 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> 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -