Android: convert xml using xslt

I want to convert some xml using an xsl file and output the result somehow (I use Android Api Level 8).

My current action looks like this, but the transformer remains zero. LogCat throws System.errusing org.apache.harmony.xml.ExpatParser$ParseException, saying that xml is not correct, but I made sure that it is.

I found a hint in LogCat that says SystemId Unknownjust before the error message above.

What am I doing wrong?

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();
        }
    }
}

This is the xml file you want to convert (source.xml)

<?xml version="1.0"?>
<!-- <?xml-stylesheet href="beatle.xsl" type="text/xsl"?> -->
<person>
 <name>
  <firstname>Paul</firstname>
  <lastname>McCartney</lastname>
 </name>
 <job>Singer</job>
 <gender>Male</gender>
</person>

And that corresponds to xsl (products.xsl)      

<xsl:template match="child::person">
 <html>
  <head>
   <title>
    <xsl:value-of select="descendant::firstname" />
    <xsl:text> </xsl:text>
    <xsl:value-of select="descendant::lastname" />
   </title>
  </head>
  <body>
   <xsl:value-of select="descendant::firstname" />
   <xsl:text> </xsl:text>
   <xsl:value-of select="descendant::lastname" />
  </body>
 </html>
</xsl:template>
</xsl:stylesheet>
+3
source share
2 answers

, : XML/XSLT res/xml res/raw XML-.

: (

0

. , XML XSL . , , . XML/XSLT?

0

Source: https://habr.com/ru/post/1774277/


All Articles