[jdom-interest] XSLT id() function doesnt work when using JDOMSource

John Caron caron at unidata.ucar.edu
Tue Aug 21 14:14:53 PDT 2001


Using jdom (b7) and xalan 2.2 D9, the XPath id() function doesnt work 
when using JDOMSource, but it does work when using StreamSource. [ I 
think in the previous xalan 2.2 D6 release id() did not work correctly]

example xsl (1) xml (2) and test code (3) follow:
--------------------
1) xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:th="http://www.unidata.ucar.edu/thredds" >

     <xsl:template match="th:catalog">
         Hey = <xsl:value-of select="id(@serverID)"/>
     </xsl:template>

</xsl:stylesheet>

--------------------
(2) xml doc

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE catalog SYSTEM 
"http://www.unidata.ucar.edu/projects/THREDDS/xml/testXSL.dtd">
<catalog name="Test XSLT" version="0.4" 
xmlns:xlink="http://www.w3.org/1999/xlink" serverID="Thredds">
     <server type="DODS" ID="Thredds" 
xml:base="http://thredds.unidata.ucar.edu:8080/dodsC/">ok!</server>
</catalog>
--------------------

this works ok if using a StreamSource, but not a JDOMSource.
the test code below gives the following output:


  Transformer class = org.apache.xalan.transformer.TransformerImpl

  Using StreamSource:

<?xml version="1.0" encoding="UTF-8"?>



         Hey = ok!
===============================


Transformer class = org.apache.xalan.transformer.TransformerImpl

  Using JDOMSource:

<?xml version="1.0" encoding="UTF-8"?>



         Hey =
===============================



------------------
(3) test code

package test.jdom;

import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
import org.jdom.transform.*;

import java.io.*;
import java.net.*;
import java.util.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.stream.StreamResult;

public class TestXSL {

   static void doTransformJDOM( String xmlURL, String styleSheet) throws 
MalformedURLException, IOException {

     URL u = new URL( xmlURL);

     Document doc = null;
     try {
       SAXBuilder builder = new SAXBuilder( true);
       doc = builder.build( u);
     } catch (JDOMException e) {
       System.out.println("***SAXBuilder Parsing error= "+ e.getMessage());
       e.printStackTrace();
       return;
     }

     Transformer transformer;
     try {
       TransformerFactory factory = TransformerFactory.newInstance();
       transformer = factory.newTransformer(new StreamSource(styleSheet));
       System.out.println(" \nTransformer class = 
"+transformer.getClass().getName());
     } catch (Exception e) {
       System.out.println("TransformerFactory failed: "+e);
       e.printStackTrace();
       return;
     }

     try {
       System.out.println(" Using JDOMSource: ");
       StreamResult out = new StreamResult( System.out);
       transformer.transform(new JDOMSource(doc), out);
       System.out.println("\n===============================");
     } catch (Exception e) {
       System.out.println("XSLT transform failed: "+e);
       e.printStackTrace();
       return;
     }

   }


   static void doTransform( String xmlURL, String styleSheet)
     throws TransformerException, TransformerConfigurationException,
            FileNotFoundException, IOException
   {
   // Use the static TransformerFactory.newInstance() method to instantiate
   // a TransformerFactory. The javax.xml.transform.TransformerFactory
   // system property setting determines the actual class to instantiate --
   // org.apache.xalan.transformer.TransformerImpl.
	TransformerFactory tFactory = TransformerFactory.newInstance();

	// Use the TransformerFactory to instantiate a Transformer that will work 
with
	// the stylesheet you specify. This method call also processes the stylesheet
   // into a compiled Templates object.
	Transformer transformer = tFactory.newTransformer(new 
StreamSource(styleSheet));
         System.out.println(" Transformer class = 
"+transformer.getClass().getName());

	// Use the Transformer to apply the associated Templates object to an XML 
document
	// (foo.xml) and write the output to a file (foo.out).
         System.out.println(" Using StreamSource: ");
	transformer.transform(new StreamSource(xmlURL), new 
StreamResult(System.out));
         System.out.println("\n===============================");
   }

   public static void main(String[] args) {

     try {
 
doTransform("http://www.unidata.ucar.edu/projects/THREDDS/xml/testXSL.xml",
 
"http://www.unidata.ucar.edu/projects/THREDDS/xml/testXSL.xsl");
     } catch (Exception e) {
       System.out.println("Main exception = \n"+ e.getMessage());
       e.printStackTrace();
     }

      try {
 
doTransformJDOM("http://www.unidata.ucar.edu/projects/THREDDS/xml/testXSL.xml",
 
"http://www.unidata.ucar.edu/projects/THREDDS/xml/testXSL.xsl");
     } catch (Exception e) {
       System.out.println("Main exception = \n"+ e.getMessage());
       e.printStackTrace();
     }

   }
}




More information about the jdom-interest mailing list