title: Android: Convert xml using xslt
body: <p>I want to transform some xml using an xsl-file and output the result somehow (I'm using Android Api Level 8).</p>
  
  <p>My current activity looks like this, but the transformer stays null.
  LogCat throws an <code>System.err</code> with <code>org.apache.harmony.xml.ExpatParser$ParseException</code>, saying the xml is not well-formed, but I made sure it is.</p>
  
  <p>I found a hint in LogCat that says <code>SystemId Unknown</code> just before the above error message.</p>
  
  <p>What am I doing wrong?</p>
  
  <pre><code>import java.io.OutputStream;
  
  import javax.xml.transform.Source;
  import javax.xml.transform.Transformer;
  import javax.xml.transform.TransformerConfigurationException;
  import javax.xml.transform.TransformerException;
  import javax.xml.transform.TransformerFactory;
  import javax.xml.transform.TransformerFactoryConfigurationError;
  import javax.xml.transform.stream.StreamResult;
  import javax.xml.transform.stream.StreamSource;
  
  import android.app.Activity;
  import android.os.Bundle;
  
  public class XsltTester extends Activity {
  
      private static String TAG = XsltTester.class.getSimpleName();
  
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
  
          try {
  
              Source xmlSource = new StreamSource(this.getResources().openRawResource(R.xml.source));
              Source xsltSource = new StreamSource(this.getResources().openRawResource(R.xml.products));
  
              TransformerFactory transFact = TransformerFactory.newInstance();
              Transformer trans = transFact.newTransformer(xsltSource);
              OutputStream output = new StringOutputStream();
              StreamResult result = new StreamResult(output);
              trans.transform(xmlSource, result);
  
          } catch (TransformerConfigurationException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } catch (TransformerFactoryConfigurationError e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } catch (TransformerException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>This is the xml-file to be transformed (source.xml)</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;!-- &lt;?xml-stylesheet href="beatle.xsl" type="text/xsl"?&gt; --&gt;
  &lt;person&gt;
   &lt;name&gt;
    &lt;firstname&gt;Paul&lt;/firstname&gt;
    &lt;lastname&gt;McCartney&lt;/lastname&gt;
   &lt;/name&gt;
   &lt;job&gt;Singer&lt;/job&gt;
   &lt;gender&gt;Male&lt;/gender&gt;
  &lt;/person&gt;
  </code></pre>
  
  <p>And this is the corresponding xsl (products.xsl)
      
      </p>
  
  <pre><code>&lt;xsl:template match="child::person"&gt;
   &lt;html&gt;
    &lt;head&gt;
     &lt;title&gt;
      &lt;xsl:value-of select="descendant::firstname" /&gt;
      &lt;xsl:text&gt; &lt;/xsl:text&gt;
      &lt;xsl:value-of select="descendant::lastname" /&gt;
     &lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
     &lt;xsl:value-of select="descendant::firstname" /&gt;
     &lt;xsl:text&gt; &lt;/xsl:text&gt;
     &lt;xsl:value-of select="descendant::lastname" /&gt;
    &lt;/body&gt;
   &lt;/html&gt;
  &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>