can't transform xml+xsl(with exslt) to html


Last Updated:

  1. aforum

    aforum New Member

    Hi guys, I am developing an app which loads a xml and a xsl and loads the resulting html, displaying it on the screen. It works perfectly when the xsl does not have exslt functions. However, when it does contain exslt functions, it doesn't work.

    The code is rather small, so I'm posting it here:

    Code (Text):
    1.  
    2. public class MainActivity extends Activity {
    3.  
    4.     @Override
    5.     protected void onCreate(Bundle savedInstanceState) {
    6.        
    7.         super.onCreate(savedInstanceState);
    8.         WebView webview = new WebView(this);
    9.         setContentView(webview);
    10.  
    11.         Source xmlSource = new StreamSource(this.getResources().openRawResource(R.raw.xml2));
    12.         Source xsltSource = new StreamSource(this.getResources().openRawResource(R.raw.xsl2));
    13.        
    14.         TransformerFactory tFactory = TransformerFactory.newInstance();
    15.         Transformer transformer = null;
    16.         ByteArrayOutputStream output = null;
    17.         StreamResult result;
    18.        
    19.         try
    20.         {
    21.             transformer = tFactory.newTransformer(xsltSource);
    22.             output = new ByteArrayOutputStream();
    23.             result = new StreamResult(output);
    24.             transformer.transform(xmlSource, result);
    25.            
    26.         } catch (TransformerException e)
    27.         {
    28.             e.printStackTrace();
    29.         }
    30.        
    31.         ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray());
    32.        
    33.        
    34.         String html = new Scanner(input,"UTF-8").useDelimiter("\\A").next();
    35.        
    36.         webview.loadData(html, "text/html", "UTF-8");
    37.     }
    38.  
    39.     @Override
    40.     public boolean onCreateOptionsMenu(Menu menu) {
    41.         // Inflate the menu; this adds items to the action bar if it is present.
    42.         getMenuInflater().inflate(R.menu.main, menu);
    43.         return true;
    44.     }
    45.  
    46. }
    47.  
    I have attached a zip file containing 5 files: xml,xsl, xml2,xsl2, logcat.
    I get the errors with xml2 and xsl2.

    Please help me, I've been trying to make it work for hours...I've tried many stuff, different codes...but it doesn't seem to have anything to do with my code.

    Thank you for your time.

    Attached Files:


    Advertisement
  2. aforum

    aforum New Member

    I forgot to mention 2 things: I'm using the eclipse IDE, ADT bundle thing. In addition, I have tried an equivalent code in netbeans:

    Code (Text):
    1.  
    2. public class JavaApplication4 {
    3.  
    4.     /**
    5.      * @param args the command line arguments
    6.      */
    7.     public static void main(String[] args) throws TransformerConfigurationException, TransformerException, FileNotFoundException {
    8.        Source xmlSource = new StreamSource("xml2.xml");
    9.        Source xsltSource = new StreamSource("xsl2.xsl");
    10.        
    11.        TransformerFactory tFactory = TransformerFactory.newInstance();
    12.        Transformer transformer = tFactory.newTransformer(xsltSource);
    13.        
    14.        ByteArrayOutputStream output = new ByteArrayOutputStream();
    15.        StreamResult result = new StreamResult(output);
    16.        transformer.transform(xmlSource, result);
    17.        
    18.        ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray());
    19.        String html = new Scanner(input,"UTF-8").useDelimiter("\\A").next();
    20.        
    21.        System.out.println(html);
    22.     }
    23. }
    24.  
    which, to my surprise, works for both xml and xsl.

Share This Page