<xsl:output method="html" encoding="UTF-8">

解决方案 »

  1.   

    在xsl文件中加了这句
    <xsl:output method="html" encoding="UTF-8">还是不对啊
    输出变成:<?xml version="1.0" encoding="UTF-8"?>
    <html xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><head /><body>
    <tr><td><input value="SqlServer" /></td><td><input value="com.microsoft.jdbc.sqlserver.SQLServerDriver" /></td><td><input value="jdbc:microsoft:sqlserver://127.0.0.1:1433" /></td><td><input value="UserName" /></td><td><input value="UserPassword" /></td></tr>
    <tr><td><input value="Access" /></td><td><input value="jdbc.odbc.JdbcOdbcDriver" /></td><td><input value="c:\demo.mdb" /></td><td><input value="UserName" /></td><td><input value="UserPassword" /></td></tr>
    </body></html>
      

  2.   

    转换代码:import org.jdom.*;
    import org.jdom.input.*;
    import org.jdom.output.*;
    import org.jdom.transform.*;import javax.xml.transform.*;
    import javax.xml.transform.stream.*;import java.io.*;
    import java.io.IOException;public class XMLToHTML {  public static void main (String[] args) {
        if (args.length > 0) {
           forTest (args[0]);
        }
      }  private static void forTest (String fileName) {
        try {
          SAXBuilder saxBuilder = new SAXBuilder (false);
          Document doc = saxBuilder.build (new File(fileName));      Transformer transformer = TransformerFactory.newInstance().newTransformer(new StreamSource("E:\\CONFIG.xslt"));
          JDOMSource jdomSource = new JDOMSource(doc);
          JDOMResult jdomResult = new JDOMResult();
          transformer.transform (jdomSource,jdomResult);
          Document doc2 = jdomResult.getDocument ();      XMLOutputter xmlOutputter = new XMLOutputter();
          xmlOutputter.output (doc, System.out);
          xmlOutputter.output (doc2, System.out);      xmlOutputter.output (doc2, new FileOutputStream("e:\\xslout.html"));
        } catch (JDOMException e) {
          System.err.println (e);
        } catch (IOException e) {
          System.err.println (e);
        } catch (Exception e) {
          System.err.println (e);
        }
      }
    }====================================================================
    ==================================xsl
    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <xsl:output method="html" encoding="utf-8"/>
    <xsl:template match="/">
    <html>
    <head/>
    <body>
    <xsl:apply-templates/>
    </body>
    </html>
    </xsl:template>
    <xsl:template match="Config">
    <xsl:if test="position()=1">
    <xsl:text disable-output-escaping="yes">&lt;table border="1"&gt;</xsl:text>
    </xsl:if>
    <xsl:if test="position()=1">
    <thead>
    <tr>
    <td>Id</td>
    <td>DriverName</td>
    <td>ConnectionURL</td>
    <td>UserName</td>
    <td>UserPassword</td>
    </tr>
    </thead>
    </xsl:if>
    <xsl:if test="position()=1">
    <xsl:text disable-output-escaping="yes">&lt;tbody&gt;</xsl:text>
    </xsl:if>
    <tr>
    <td>
    <xsl:for-each select="@Id">
    <input>
    <xsl:attribute name="value"><xsl:value-of select="." /></xsl:attribute>
    </input>
    </xsl:for-each>
    </td>
    <td>
    <xsl:for-each select="DriverName">
    <input>
    <xsl:attribute name="value"><xsl:value-of select="." /></xsl:attribute>
    </input>
    </xsl:for-each>
    </td>
    <td>
    <xsl:for-each select="ConnectionURL">
    <input>
    <xsl:attribute name="value"><xsl:value-of select="." /></xsl:attribute>
    </input>
    </xsl:for-each>
    </td>
    <td>
    <xsl:for-each select="UserName">
    <input>
    <xsl:attribute name="value"><xsl:value-of select="." /></xsl:attribute>
    </input>
    </xsl:for-each>
    </td>
    <td>
    <xsl:for-each select="UserPassword">
    <input>
    <xsl:attribute name="value"><xsl:value-of select="." /></xsl:attribute>
    </input>
    </xsl:for-each>
    </td>
    </tr>
    <xsl:if test="position()=last()">
    <xsl:text disable-output-escaping="yes">&lt;/tbody&gt;</xsl:text>
    </xsl:if>
    <xsl:if test="position()=last()">
    <xsl:text disable-output-escaping="yes">&lt;/table&gt;</xsl:text>
    </xsl:if>
    </xsl:template>
    </xsl:stylesheet>============================================================================
    =================== xml
    <?xml version="1.0" encoding="UTF-8"?>
    <!-- edited with XMLSPY v5 U (http://www.xmlspy.com) by administrator (CreatxrHeaven) -->
    <?xml-stylesheet type="text/xsl" href=".\CONFIG.xslt"?>
    <Configurations xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="./CONFIG.xsd" Default="Access">
    <Config Id="SqlServer">
    <DriverName>com.microsoft.jdbc.sqlserver.SQLServerDriver</DriverName>
    <ConnectionURL>jdbc:microsoft:sqlserver://127.0.0.1:1433</ConnectionURL>
    <UserName>UserName</UserName>
    <UserPassword>UserPassword</UserPassword>
    </Config>
    <Config Id="Access">
    <DriverName>jdbc.odbc.JdbcOdbcDriver</DriverName>
    <ConnectionURL>c:\demo.mdb</ConnectionURL>
    <UserName>UserName</UserName>
    <UserPassword>UserPassword</UserPassword>
    </Config>
    </Configurations>=============================================================================================================== out html
    <?xml version="1.0" encoding="UTF-8"?>
    <html xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><head /><body>
    <tr><td><input value="SqlServer" /></td><td><input value="com.microsoft.jdbc.sqlserver.SQLServerDriver" /></td><td><input value="jdbc:microsoft:sqlserver://127.0.0.1:1433" /></td><td><input value="UserName" /></td><td><input value="UserPassword" /></td></tr>
    <tr><td><input value="Access" /></td><td><input value="jdbc.odbc.JdbcOdbcDriver" /></td><td><input value="c:\demo.mdb" /></td><td><input value="UserName" /></td><td><input value="UserPassword" /></td></tr>
    </body></html>================================================================
    居然table标志没了,怪异
      

  3.   

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <xsl:template match="/">
    <html>
    <head/>
    <body>
              <table border="1">
    <thead>
    <tr>
    <td>Id</td>
    <td>DriverName</td>
    <td>ConnectionURL</td>
    <td>UserName</td>
    <td>UserPassword</td>
    </tr>
    </thead>
                            <tbody>
                             <xsl:apply-templates/>
                            </tbody>
                            </table>
    </body>
    </html>
    </xsl:template>
    <xsl:template match="Config">
    <tr>
    <td>
    <xsl:for-each select="@Id">
    <input>
    <xsl:attribute name="value"><xsl:value-of select="." /></xsl:attribute>
    </input>
    </xsl:for-each>
    </td>
    <td>
    <xsl:for-each select="DriverName">
    <input>
    <xsl:attribute name="value"><xsl:value-of select="." /></xsl:attribute>
    </input>
    </xsl:for-each>
    </td>
    <td>
    <xsl:for-each select="ConnectionURL">
    <input>
    <xsl:attribute name="value"><xsl:value-of select="." /></xsl:attribute>
    </input>
    </xsl:for-each>
    </td>
    <td>
    <xsl:for-each select="UserName">
    <input>
    <xsl:attribute name="value"><xsl:value-of select="." /></xsl:attribute>
    </input>
    </xsl:for-each>
    </td>
    <td>
    <xsl:for-each select="UserPassword">
    <input>
    <xsl:attribute name="value"><xsl:value-of select="." /></xsl:attribute>
    </input>
    </xsl:for-each>
    </td>
    </tr>
    </xsl:template>
    </xsl:stylesheet>