[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: parsing xsl file in java


parse xsl file
Zahida Parveen wrote:
 > Hi,
 >
 > Can any one please help me by guiding how can I parse an xsl file through
 > Java Servlets and replace a node's attribute in it.
 >
 > Waiting for assistance,
 > zahida


Here is the source of a little command line Java program called Moxie [1]. It is 
an interface to the JAXP XSLT engine. I'd guess you could add servlet classes to 
this program and get it to work. You could pass in the source and stylesheet in 
other ways besides reading it in from args.

/*
  * Moxie JAXP XSLT processor
  */

import java.io.File;
import java.io.FileOutputStream;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

public class Moxie {

     public static void main(String[] args) throws Exception {

        /* Output file flag */
        boolean file = false;

        /* Default system property for Xalan processor */
        System.setProperty("javax.xml.transform.TransformerFactory",
            "org.apache.xalan.processor.TransformerFactoryImpl");

        /* Usage strings */
        String info = "Moxie JAXP XSLT processor";
        String usage = "\nUsage: java -jar moxie.jar";
        String parms = " source stylesheet [result]";

        /* Test arguments */
        if (args.length == 0) {
            System.out.println(info + usage + parms);
            System.exit(1);
        } else if (args.length == 3) {
            file = true;
        } else if (args.length > 3) {
            System.out.println("Too many arguments; exit.");
            System.exit(1);
        }

        /* XML source document and stylesheet */
        File source = new File(args[0]);
        File stylesheet = new File(args[1]);

        /* Set up source and result streams */
        StreamSource src = new StreamSource(source);
        StreamSource style = new StreamSource(stylesheet);
        StreamResult out;
        if (file) {
            FileOutputStream outFile = new FileOutputStream(args[2]);
            out = new StreamResult(outFile);
        } else {
            out = new StreamResult(System.out);
        }

        /* Create transformer */
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer xf = factory.newTransformer(style);

        /* Set output encoding property */
        xf.setOutputProperty(OutputKeys.ENCODING, "US-ASCII"); // encoding
        xf.setOutputProperty(OutputKeys.INDENT, "yes");        // indent

        /* Perform the transformation */
        xf.transform(src, out);

     }

}

I can only guess what you mean by "replace a node's attribute in it." I suspect 
you want to process an XML document with your servlet, which, if you use this 
code, will require an XSLT stylesheet on the server that contains something like 
this (untested):

<xsl:template match="mynode">
  <xsl:copy>
   <xsl:attribute name="{name(@myatt)}">newvalue</xsl:attribute>
  </xsl:copy>
</xsl:template>

If you turn this code into a servlet, I'd like to see it. Best of luck.

Mike


[1] from Chapter 17 of Learning XSLT (O'Reilly, 2003).


PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
 

Stylus Studio has published XML-DEV in RSS and ATOM formats, enabling users to easily subcribe to the list from their preferred news reader application.


Stylus Studio Sponsored Links are added links designed to provide related and additional information to the visitors of this website. they were not included by the author in the initial post. To view the content without the Sponsor Links please click here.

Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.