I am new to XSLT. I am trying to transform one XML document to another. My first step is to try an do a really simple example. I want to see the results to my simple example in a browser, but neither IE7 or Firefox is not returning the results I expect. My XML is:

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<catalog>
	<cd>
		<title>Empire Burlesque</title>
		<artist>Bob Dylan</artist>
		<country>USA</country>
		<company>Columbia</company>
		<price>10.90</price>
		<year>1985</year>
	</cd>
	<cd>
		<title>Hide your heart</title>
		<artist>Bonnie Tyler</artist>
		<country>UK</country>
		<company>CBS Records</company>
		<price>9.90</price>
		<year>1988</year>
	</cd>
</catalog>

I simply want to produce a simple output of:

<root>
	<singer>Bob Dylan</singer>
	<singer>Bonnie Tyler</singer>
</root>

My XSLT is:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
	<xsl:template match="/">
                                <xsl:element name="root">
		<xsl:for-each select="catalog/cd">
		    <xsl:element name="singer">
      			<xsl:value-of select="artist" />
    		    </xsl:element>
		    <br />
		</xsl:for-each>
	</xsl:template>
</xsl:stylesheet>

IE7 says Access is denied. Error processing resource... Firefox displays all the data as text (no xml tags).

To top it all off, my java program wont compile the style sheet. I want to get this simple sample going so I can move on to the actual XML files that I wish to transform to update databases. Please help, I must be missing something within the XSL.

You are still thinking in terms of procedural languages such as C or Java. Here is how to do it in a declarative language such as XSL.

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/">
   <xsl:element name="root">
      <xsl:apply-templates select="catalog/cd/artist"/>
   </xsl:element>
</xsl:template>

<xsl:template match="catalog/cd/artist">
   <xsl:element name="singer" >
      <xsl:value-of select="."/>
   </xsl:element>
</xsl:template>

</xsl:stylesheet>

Thanks for your reply. I probably am still thinking in terms of precedural languages. I changed my XSL document to what you said, however, IE7 still tells me 'Access is denied. Error Processing resource...<file path> .

Firefox still won't display the data correctly, which is odd.
Is there a security setting on IE?

My Java program indicates that it can't compile the the stylesheet. My code for my program is (only a simple program for testing purposes):

import java.io.File;

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 SimpleXMLTransform {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
	        String inXML = "C:/cdcatalog.xml";
	        String inXSL = "C:/test.xsl";
	        String outTXT = "C:/test.xml";

	        SimpleXMLTransform st = new SimpleXMLTransform();
	        try {
	            st.transform(inXML,inXSL,outTXT);
	        } catch(TransformerConfigurationException e) {
	            System.err.println("Invalid factory configuration");
	            System.err.println(e);
	        } catch(TransformerException e) {
	            System.err.println("Error during transformation");
	            System.err.println(e);
	        }

	}

    public void transform(String inXML,String inXSL,String outTXT)
    throws TransformerConfigurationException,
        TransformerException {

    	TransformerFactory factory = TransformerFactory.newInstance();

        	StreamSource xslStream = new StreamSource(inXSL);
        	Transformer transformer = factory.newTransformer(xslStream);
        	transformer.setErrorListener(new MyErrorListener());

        	StreamSource in = new StreamSource(inXML);
        	StreamResult out = new StreamResult(outTXT);
        	transformer.transform(in,out);
        	System.out.println("The generated XML file is:" + outTXT);
    		
    }

}

class MyErrorListener implements ErrorListener {
    public void warning(TransformerException e)
                throws TransformerException {
        show("Warning",e);
        throw(e);
    }
    public void error(TransformerException e)
                throws TransformerException {
        show("Error",e);
        throw(e);
    }
    public void fatalError(TransformerException e)
                throws TransformerException {
        show("Fatal Error",e);
        throw(e);
    }
    private void show(String type,TransformerException e) {
        System.out.println(type + ": " + e.getMessage());
        if(e.getLocationAsString() != null)
            System.out.println(e.getLocationAsString());
        
    }
}

I would appreciate any other feedback/assistance.

Is there a good tutorial for XML transformation? The w3c one seems to focus on transforming to HTML, not to another XML doucment.

I have got it working now. You are 100% correct. Thankyou for your assistance.

There does seems to be an issue with long file paths. When I copied my documents to c:\tmp, then re-ran my program, it worked. I am still having an issue with IE7 though. When I put the reference in to the xsl in the xml file, IE7 still throws the error previously mentioned.

Does anyone know why IE7 would have an issue with displaying the output?

However, my java program works as expected, and produces the desired result.

Try this in .xsl file

<?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:template match="/"> <root> <xsl:for-each select="catalog/cd"> <singer><xsl:value-of select = "artist" /></singer> </xsl:for-each> </root> </xsl:template> </xsl:stylesheet>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.