OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.

 


Help: OASIS Mailing Lists Help | MarkMail Help

 


 

   Re: [xml-dev] parsing xsl file in java

[ Lists Home | Date Index | Thread Index ]

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).





 

News | XML in Industry | Calendar | XML Registry
Marketplace | Resources | MyXML.org | Sponsors | Privacy Statement

Copyright 2001 XML.org. This site is hosted by OASIS