使用xslt将xml转换为html

时间:2021-08-19 21:50:11

What I am doing: I am trying to transform xml to html using xslt.

我在做什么:我正在尝试使用xslt将xml转换为html。


Question: The program is executing without any error, it is gproducing the output file also, but it does not transform the xml to html. My guess is that the for loop in the xsl is not fetching the data.

问题:程序正在执行而没有任何错误,它也会生成输出文件,但它不会将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>

2 个解决方案

#1


12  

XML is very unforgiving. This:

XML非常无情。这个:

     <xsl:for-each select="language-list/language">

needs to be:

需要是:

     <xsl:for-each select="languages-list/language">

#2


0  

Just fixing language(s)-list does not make the program error free. It fails with error as below:

只修复语言-list不会使程序错误。它失败,错误如下:

Error on line 7 of FileTwo.xsl:
  java.lang.IllegalArgumentException: URI scheme is not "file"
TransformerException
net.sf.saxon.trans.XPathException: java.lang.IllegalArgumentException: URI scheme is not "file"

This is misleading because it does not point to the actual problem. The problem is with "outputHTML" it should be of a type File or FileOutputStream.

这是误导性的,因为它没有指出实际问题。问题在于“outputHTML”,它应该是File或FileOutputStream类型。

I have tried with File and it worked. So this statement:

我尝试过使用File并且工作正常。所以这句话:

StreamResult out = new StreamResult(outputHTML);

is rewritten as:

被重写为:

StreamResult out = new StreamResult(new File(outputHTML));

Ofcourse import java.io.File

Ofcourse import java.io.File

Using FileOutputStream needs appropriate code tweaking and import statements.

使用FileOutputStream需要适当的代码调整和导入语句。

#1


12  

XML is very unforgiving. This:

XML非常无情。这个:

     <xsl:for-each select="language-list/language">

needs to be:

需要是:

     <xsl:for-each select="languages-list/language">

#2


0  

Just fixing language(s)-list does not make the program error free. It fails with error as below:

只修复语言-list不会使程序错误。它失败,错误如下:

Error on line 7 of FileTwo.xsl:
  java.lang.IllegalArgumentException: URI scheme is not "file"
TransformerException
net.sf.saxon.trans.XPathException: java.lang.IllegalArgumentException: URI scheme is not "file"

This is misleading because it does not point to the actual problem. The problem is with "outputHTML" it should be of a type File or FileOutputStream.

这是误导性的,因为它没有指出实际问题。问题在于“outputHTML”,它应该是File或FileOutputStream类型。

I have tried with File and it worked. So this statement:

我尝试过使用File并且工作正常。所以这句话:

StreamResult out = new StreamResult(outputHTML);

is rewritten as:

被重写为:

StreamResult out = new StreamResult(new File(outputHTML));

Ofcourse import java.io.File

Ofcourse import java.io.File

Using FileOutputStream needs appropriate code tweaking and import statements.

使用FileOutputStream需要适当的代码调整和导入语句。