[jdom-interest] Name spaces and XSLT

Hans Verschoor hanjen at xs4all.nl
Mon Jun 17 13:59:02 PDT 2002


Hi, I just started using the JDOM and I must say, it's really very much
easier and more versatile than the DOM.

I am a relatively newbie in the XML area and I encountered the following
problem of which I don't know if it may be some silly thing I did:
I build a document from scratch and add two elements having their own
namespace. When I pass this document to an XSLT transformer (Xalan in my
case) I get an exception indicating that the namespace cannot be resolved.
My questions are: Did I do something wrong in my coding (probably),  is
something going wrong in the implicit conversion from JDOM to DOM or is
there something wrong with the XSL source ?
I added the source of my test program, although it's pretty long. I also
added two scripts to test with. If you give "true" as the argument the test
crashes, else the test runs without namespaces and should work fine.
I test with JAVA SDK 1.4

Thanx for any suggestions,
Hans

Widows:

@if "%CLASSPATH%"=="" goto noclspath
@set CLASSPATH=C:\jdom-b8\build\jdom.jar;%CLASSPATH%
@goto testit
:noclspath
@set CLASSPATH=C:\jdom-b8\build\jdom.jar
:testit
java Test %1
@pause

Linux:

#!/bin/sh
JAVA_HOME=/usr/local/j2sdk1.4.0
export JAVA_HOME
JVS_SE_RUNDIR=$JAVA_HOME/bin
export JVS_SE_RUNDIR
SVCLASSPATH=$CLASSPATH
CLASSPATH=/usr/local/jdom/jdom.jar
if [ -n $SVCLASSPATH ]; then
  CLASSPATH=$SVCLASSPATH:$CLASSPATH
fi
export CLASSPATH
echo "Test JDOM functionality"
${JVS_SE_RUNDIR)/java Test ${1}

The Java source.

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

import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;

import java.io.*;

// -------------------------------------------------------------------------
--
/**
 * The testing class.
 * @version 1.0
 * @author Hans Verschoor
 */
public abstract class Test {



// -------------------------------------------------------------------------
  private Test () {
  }



// -------------------------------------------------------------------------
  /**
   * Starting the server.
   */
  public static void main ( String[] args ) {

    int exitCode = 0;

    try {

      boolean useNameSpaces = false;
      if ( args.length > 0 ) {
        if ( args[0].length() > 0 ) {
          switch ( args[0].charAt(0) ) {
            case 't':
            case 'T':
            case '1':
            case 'Y':
            case 'y':
              useNameSpaces = true;
              break;
            default:
              useNameSpaces = false;
              break;
          }
        }
      }

      //
DOCUMENT -------------------------------------------------------------
      //
      org.jdom.adapters.JAXPDOMAdapter  jaxpAdapter =
                                       new org.jdom.adapters.JAXPDOMAdapter
();
      org.w3c.dom.Document      domDoc      = jaxpAdapter.createDocument();
      org.jdom.input.DOMBuilder jdomBuilder = new org.jdom.input.DOMBuilder
();
      org.jdom.Document         jdomDoc     = jdomBuilder.build ( domDoc );

      //
DOCTYPE --------------------------------------------------------------
      //
      org.jdom.DocType jdomDocType  = new org.jdom.DocType ( "tester" );
      jdomDoc.setDocType ( jdomDocType );

      //
ROOT -----------------------------------------------------------------
      //
      org.jdom.Element jdomRootElem = new org.jdom.Element ( "tester" );
      jdomDoc.setRootElement ( jdomRootElem );

      //
ABC ------------------------------------------------------------------
      //
      org.jdom.Element jdomAbcElem = new org.jdom.Element ( "abc" );
      jdomRootElem.addContent ( jdomAbcElem );

      org.jdom.Element jdomAbcHolder = null;
      if ( useNameSpaces == true ) {
        org.jdom.Namespace jdomAbcNameSpace =
org.jdom.Namespace.getNamespace
                                                  ( "abc" ,"urn:abc" );
        jdomAbcElem.setNamespace ( jdomAbcNameSpace );

        jdomAbcHolder = new org.jdom.Element ( "valueholder"
                                               ,jdomAbcNameSpace );
      } else {
        jdomAbcHolder = new org.jdom.Element ( "abc-valueholder" );
      }

      jdomAbcElem.addContent   ( jdomAbcHolder );
      jdomAbcHolder.addContent ( "The ABC text value" );

      //
PQR ------------------------------------------------------------------
      //
      org.jdom.Element          jdomPqrElem = new org.jdom.Element (
"pqr" );
      jdomRootElem.addContent ( jdomPqrElem );

      org.jdom.Element jdomPqrHolder = null;
      if ( useNameSpaces == true ) {
        org.jdom.Namespace jdomPqrNameSpace =
org.jdom.Namespace.getNamespace
                                                  ( "pqr" ,"urn:pqr" );
        jdomPqrElem.setNamespace ( jdomPqrNameSpace );

        jdomPqrHolder = new org.jdom.Element ( "valueholder"
                                               ,jdomPqrNameSpace );
      } else {
        jdomPqrHolder = new org.jdom.Element ( "pqr-valueholder" );
      }

      jdomPqrElem.addContent   ( jdomPqrHolder );
      jdomPqrHolder.addContent ( "The PQR text value" );


      // OUTPUT JDOM
IMAGE ----------------------------------------------------
      //
      org.jdom.output.XMLOutputter jdomXmlOutput =
                             new org.jdom.output.XMLOutputter ( "  "
,true );
      System.out.println   ( "===========> ORG.JDOM IMAGE" );
      jdomXmlOutput.output ( jdomDoc ,System.out );
      System.out.println   ( "<=========== ORG.JDOM IMAGE" );
      System.out.println   ();

      // CONVERT TO
HTML ------------------------------------------------------

      String xsltImage = null;

      if ( useNameSpaces == true ) {

        xsltImage = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n"
                  + "<xsl:stylesheet version=\"2.0\"\n"
                  + "  xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\"\n"
                  + "  xmlns=\"http://www.w3.org/1999/xhtml\">\n"
                  + "<xsl:output method=\"html\"/>\n"

                  + "<xsl:template match=\"tester\">\n"
                  + "<html>\n"
                  + "<head>\n"
                  + "<title>JDOM tester</title>\n"
                  + "</head>\n"
                  + "<body>\n"
                  + "<xsl:apply-templates select=\"abc:abc\"/>\n"
                  + "<xsl:apply-templates select=\"pqr:pqr\"/>\n"
                  + "</body>\n"
                  + "</html>\n"
                  + "</xsl:template>\n"

                  + "<xsl:template match=\"abc:abc\">\n"
                  + "<br/>start ABC GROUP\n"
                  + "<xsl:apply-templates select=\"abc:valueholder\"/>\n"
                  + "<br/>end ABC GROUP\n"
                  + "</xsl:template>\n"

                  + "<xsl:template match=\"pqr:pqr\">\n"
                  + "<br/>start PQR GROUP\n"
                  + "<xsl:apply-templates select=\"pqr:valueholder\"/>\n"
                  + "<br/>end PQR GROUP\n"
                  + "</xsl:template>\n"

                  + "<xsl:template match=\"abc:valueholder\">\n"
                  + "<br/>ABC value = <b><xsl:value-of select=\".\"/></b>\n"
                  + "</xsl:template>\n"

                  + "<xsl:template match=\"pqr:valueholder\">\n"
                  + "<br/>PQR value = <b><xsl:value-of select=\".\"/></b>\n"
                  + "<xsl:value-of select=\".\"/>\n"
                  + "</xsl:template>\n"

                  + "</xsl:stylesheet>\n";
      } else {

        xsltImage = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n"
                  + "<xsl:stylesheet version=\"2.0\"\n"
                  + "  xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\"\n"
                  + "  xmlns=\"http://www.w3.org/1999/xhtml\">\n"
                  + "<xsl:output method=\"html\"/>\n"

                  + "<xsl:template match=\"tester\">\n"
                  + "<html>\n"
                  + "<head>\n"
                  + "<title>JDOM namespace tester</title>\n"
                  + "</head>\n"
                  + "<body>\n"
                  + "<xsl:apply-templates select=\"abc\"/>\n"
                  + "<xsl:apply-templates select=\"pqr\"/>\n"
                  + "</body>\n"
                  + "</html>\n"
                  + "</xsl:template>\n"

                  + "<xsl:template match=\"abc\">\n"
                  + "<br/>start ABC GROUP\n"
                  + "<xsl:apply-templates select=\"abc-valueholder\"/>\n"
                  + "<br/>end ABC GROUP\n"
                  + "</xsl:template>\n"

                  + "<xsl:template match=\"pqr\">\n"
                  + "<br/>start PQR GROUP\n"
                  + "<xsl:apply-templates select=\"pqr-valueholder\"/>\n"
                  + "<br/>end PQR GROUP\n"
                  + "</xsl:template>\n"

                  + "<xsl:template match=\"abc-valueholder\">\n"
                  + "<br/>ABC value = <b><xsl:value-of select=\".\"/></b>\n"
                  + "</xsl:template>\n"

                  + "<xsl:template match=\"pqr-valueholder\">\n"
                  + "<br/>PQR value = <b><xsl:value-of select=\".\"/></b>\n"
                  + "</xsl:template>\n"

                  + "</xsl:stylesheet>\n";
      }

      System.out.println ( "===========> XSLT IMAGE" );
      System.out.print   ( xsltImage );
      System.out.println ( "<=========== XSLT IMAGE" );
      System.out.println ();

      javax.xml.transform.stream.StreamSource xsltSource =
                     new javax.xml.transform.stream.StreamSource
                                       ( new StringReader ( xsltImage ) );

      javax.xml.transform.TransformerFactory  xsltTransformerFactory =
                     javax.xml.transform.TransformerFactory.newInstance();

      javax.xml.transform.Transformer         xsltTransformer =
                     xsltTransformerFactory.newTransformer ( xsltSource );

      javax.xml.transform.stream.StreamResult htmlOutput =
                      new javax.xml.transform.stream.StreamResult
                                                           ( System.out );

      org.jdom.transform.JDOMSource jdomSource =
                      new org.jdom.transform.JDOMSource ( jdomDoc );

      System.out.println   ( "===========> HTML IMAGE" );
      xsltTransformer.transform ( jdomSource ,htmlOutput );
      System.out.println   ( "<=========== HTML IMAGE" );
      System.out.println   ();

    } catch ( Exception anyUncatchedException ) {

      System.out.println ( "> > > > > > > > > > Uncatched exception" );
      anyUncatchedException.printStackTrace ( System.out );
      System.out.println ( "< < < < < < < < < < Uncatched exception" );

      exitCode = 3;
    }

    System.out.println (    "> > > > > > > > > > Exit code: "
                          + Integer.toString ( exitCode ) );

    System.exit ( exitCode );
  }

} // Test.java





More information about the jdom-interest mailing list