我在做什么:我正在尝试使用xslt将xml转换为HTML.
问题:程序正在执行而没有任何错误,它也会生成输出文件,但它不会将xml转换为HTML.我的猜测是xsl中的for循环不是获取数据.
XSLTTest.java
package JavaXSLTExample;import javax.xml.transform.ErrorListener;import javax.xml.transform.transformer;import javax.xml.transform.transformerConfigurationException;import javax.xml.transform.transformerException;import javax.xml.transform.transformerFactory;import javax.xml.transform.stream.StreamResult;import javax.xml.transform.stream.StreamSource;public class XSLTTest {public static voID main(String[] args){ /*if (args.length != 3) { System.err.println("give command as follows : "); System.err.println("XSLTTest data.xml converted.xsl converted.HTML"); return; }*/ String dataxML = "C:\Users\Devrath\Desktop\XSL\fileOne.xml"; String inputXSL = "C:\Users\Devrath\Desktop\XSL\fileTwo.xsl"; String outputHTML = "C:\Users\Devrath\Desktop\XSL\output1.HTML"; XSLTTest st = new XSLTtest(); try { st.transform(dataxML, inputXSL, outputHTML); } catch (transformerConfigurationException e) { System.err.println("transformerConfigurationException"); System.err.println(e); } catch (transformerException e) { System.err.println("transformerException"); System.err.println(e); } } public voID transform(String dataxML, String inputXSL, String outputHTML) throws transformerConfigurationException, transformerException { transformerFactory factory = transformerFactory.newInstance(); StreamSource xslStream = new StreamSource(inputXSL); transformer transformer = factory.newtransformer(xslStream); StreamSource in = new StreamSource(dataxML); StreamResult out = new StreamResult(outputHTML); transformer.transform(in, out); System.out.println("The generated HTML file is:" + outputHTML); }}
fileOne.xml
<languages-List> <language> <name>Kannada</name> <region>Karnataka</region> <users>38M</users> <family>Dravidian</family> </language> <language> <name>Telugu</name> <region>Andra Pradesh</region> <users>74M</users> <family>Dravidian</family> </language> <language> <name>Tamil</name> <region>TamilNadu</region> <users>61M</users> <family>Dravidian</family> </language> <language> <name>Malayalam</name> <region>Kerela</region> <users>33M</users> <family>Dravidian</family> </language> <language> <name>hindi</name> <region>Andaman and Nicobar Islands, north india, Parts of north east</region> <users>442M</users> <family>Indo Aryan</family> </language> <language> <name>Assamese</name> <region>Assam, Arunachal Pradesh</region> <users>13M</users> <family>Indo Aryan</family> </language></languages-List>
fileTwo.xsl
<?xml version="1.0" enCoding="ISO-8859-1"?><xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/transform"> <xsl:template match="/"> <HTML> <body> <h1>Indian Languages details</h1> <table border="1"> <tr> <th>Language</th> <th>Family/Origin</th> <th>No. of speakers</th> <th>Region</th> </tr> <xsl:for-each select="language-List/language"> <tr> <td><xsl:value-of select="name"/></td> <td><xsl:value-of select="family"/></td> <td><xsl:value-of select="users"/></td> <td><xsl:value-of select="region"/></td> </tr> </xsl:for-each> </table> </body> </HTML> </xsl:template></xsl:stylesheet>
Output.HTML
<HTML><body><h1>Indian Languages details</h1><table border="1"><tr><th>Language</th><th>Family/Origin</th><th>No. of speakers</th><th>Region</th></tr></table></body></HTML>
解决方法:
XML非常无情.这个:
<xsl:for-each select="language-List/language">
需要是:
<xsl:for-each select="languages-List/language">
总结 以上是内存溢出为你收集整理的java – 使用xslt将xml转换为html全部内容,希望文章能够帮你解决java – 使用xslt将xml转换为html所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)