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

  • Replies:1
AndroidPitForumUser
  • Forum posts: 2

Jul 15, 2013, 7:10:49 PM via Website

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:

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

An example of a xml and xsl that works(no exslt functions): h t t p://w w w.w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog . Just change the encoding to
1encoding="UTF-8"
.

Now the xml2 and xsl2, that doesn't work(with exslt functions):
xml2:
1<?xml version="1.0" encoding="UTF-8"?>
2<?xml-stylesheet type="text/xsl" href="xsl2.xsl"?>
3<whatever>
4</whatever>
xsl2:
1<?xml version="1.0" encoding="UTF-8"?>
2
3<xsl:stylesheet xmlns:xsl="h t t p://w w w.w3.org/1999/XSL/Transform"
4 xmlns:date="h t t p://exslt.org/dates-and-times"
5 extension-element-prefixes="date"
6 version="1.0">
7
8 <xsl:template match="/">
9 date:<xsl:value-of select="date:date()"/>
10 </xsl:template>
11
12</xsl:stylesheet>

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

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

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.

EDIT

I've added spaces on the links because I couldn't post links and also couldn't attach files.

EDIT2
logcat
107-15 15:17:11.252: E/AndroidRuntime(2678): FATAL EXCEPTION: main
207-15 15:17:11.252: E/AndroidRuntime(2678): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.teste/com.example.teste.MainActivity}: java.util.NoSuchElementException
307-15 15:17:11.252: E/AndroidRuntime(2678): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
407-15 15:17:11.252: E/AndroidRuntime(2678): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
507-15 15:17:11.252: E/AndroidRuntime(2678): at android.app.ActivityThread.access$600(ActivityThread.java:141)
607-15 15:17:11.252: E/AndroidRuntime(2678): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
707-15 15:17:11.252: E/AndroidRuntime(2678): at android.os.Handler.dispatchMessage(Handler.java:99)
807-15 15:17:11.252: E/AndroidRuntime(2678): at android.os.Looper.loop(Looper.java:137)
907-15 15:17:11.252: E/AndroidRuntime(2678): at android.app.ActivityThread.main(ActivityThread.java:5041)
1007-15 15:17:11.252: E/AndroidRuntime(2678): at java.lang.reflect.Method.invokeNative(Native Method)
1107-15 15:17:11.252: E/AndroidRuntime(2678): at java.lang.reflect.Method.invoke(Method.java:511)
1207-15 15:17:11.252: E/AndroidRuntime(2678): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
1307-15 15:17:11.252: E/AndroidRuntime(2678): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
1407-15 15:17:11.252: E/AndroidRuntime(2678): at dalvik.system.NativeStart.main(Native Method)
1507-15 15:17:11.252: E/AndroidRuntime(2678): Caused by: java.util.NoSuchElementException
1607-15 15:17:11.252: E/AndroidRuntime(2678): at java.util.Scanner.next(Scanner.java:1007)
1707-15 15:17:11.252: E/AndroidRuntime(2678): at java.util.Scanner.next(Scanner.java:980)
1807-15 15:17:11.252: E/AndroidRuntime(2678): at com.example.teste.MainActivity.onCreate(MainActivity.java:58)
1907-15 15:17:11.252: E/AndroidRuntime(2678): at android.app.Activity.performCreate(Activity.java:5104)
2007-15 15:17:11.252: E/AndroidRuntime(2678): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
2107-15 15:17:11.252: E/AndroidRuntime(2678): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
2207-15 15:17:11.252: E/AndroidRuntime(2678): ... 11 more

— modified on Jul 15, 2013, 9:36:07 PM

Reply
AndroidPitForumUser
  • Forum posts: 2

Jul 18, 2013, 4:50:41 PM via Website

I've tried the equivalent code in a java project in eclipse as well. It worked like a charm. I'm trying to make it work in the android emulator, did anyone try? Please, it's easy to set up this android project. As the code suggests, you just need to create a raw folder to store the xml and the xsl. I've got to make exslt functions work, please help. Thank you for your time.

Reply