[jdom-interest] newbie: manipulating xsl file with jdom

Ian Lea ian.lea at blackwell.co.uk
Thu Apr 26 02:12:14 PDT 2001


I suspect that Jason is correct and you are not handling the
namespaces correctly.  Enclosed below is some code which
seems to work fine on your XML, locating the html and h1
HTML elements. btw I think your h1 is in the wrong place.


import java.io.FileInputStream;
import java.util.Iterator;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.Namespace;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;


public class x {
    
    public static void main(String[] args) throws Exception {
	FileInputStream fis = new FileInputStream(args[0]);
	SAXBuilder builder = new SAXBuilder();
	Document jdoc = builder.build(fis);
	Element jroot = jdoc.getRootElement();
	Namespace xslns = Namespace
	    .getNamespace("xsl",
			  "http://www.w3.org/1999/XSL/Transform");
	System.out.println(jroot+" "+xslns);
	List l = jroot.getChildren("template", xslns);
	Iterator i = l.iterator();
	XMLOutputter xmlo = new XMLOutputter();
	while (i.hasNext()) {
	    Element el = (Element) i.next();
	    System.out.println(el);
	    Element html = el.getChild("html");
	    if (html != null) {
		System.out.print("\n\nhtml=");
		xmlo.output(html, System.out);
		System.out.println();
		Element h1 = html.getChild("h1");
		if (h1 != null) {
		    System.out.print("\n\nh1=");
		    xmlo.output(h1, System.out);
		    System.out.println();
		}
	    }
	}
    }
}


--
Ian Lea.
ian.lea at blackwell.co.uk


Alan Eustace wrote:
> 
> this is the element i'm trying to access, and the children i'd want to access are the
> html presentation tags:
> 
> <xsl:template match="bbs">
>   <html>
>    <head>
>     <title>
>      <xsl:value-of select="@title"/>
>     </title>
>    </head>
>     <h1><b><font face="Arial, Helvetica, sans-serif" color="#FFFFFF">Stylesheet style:
> default
>      </font></b></h1>
>    <body bgcolor="#000000" text="#ffffff">
>        <xsl:apply-templates select="message"/>
>     <br/>
>    </body>
>   </html>
>  </xsl:template>
> when i try to iterate through the list of <xsl:template> elements to an individual
> element i get a null.
> 
> Jason Hunter wrote:
> 
> > Is the child in a namespace?  Are you passing the proper namespace to
> > the get method?
> >
> > -jh-
> >
> > Alan Eustace wrote:
> > >
> > > hope someone can help with this. i'm trying to build xsl dynamically in
> > > a servlet, and have been using the root.getChildren() method to try to
> > > access the elements I need. I can output a list of the elements, and
> > > iterate through them, in this case returning a list of <xsl:template>
> > > elements, but when i try to access an individual element by name to
> > > alter it's child elements, i get a null.
> > > hope this isn't too vague- i can supply code and xsl file if needs be.
> > > thanks
> > > alan eustace



More information about the jdom-interest mailing list