Timo: java und xslt

Beitrag lesen

Hi,

ich glaube ich weiß jtzt warum: die SAXON 9he.jar beinhaltet kein com.icl.._Package, deswegen hat eclipse das nicht erkannt.

Jetzt nochmal einpaar andere Frage:
1)warum kann JAXP kein xslt 2.0 unterstützen?Falls doch kannst du mir bitte ein BeispielProgramm schicken.
2)hast du nicht zufällig ein Java_Programm, das xslt 2.0 unterstützt?
mit Java_Programm meine ich sowas ähnliches siehe Code:

Sign In/My Account | View Cart

O'Reilly Home
 Community
 Books & Videos
 Safari Books Online
 Conferences
 Training
 School of Technology
 About

Search

Search Tips

Print Subscribe to ONJava Subscribe to Newsletters
ShareThisXSLT Processing with Java
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9

SAXON, Xalan, or TrAX?
As the previous examples show, SAXON and Xalan have many similarities. While similarities make learning the various APIs easy, they do not result in portable code. If you write code directly against either of these interfaces, you lock yourself into that particular implementation unless you want to rewrite your application.

The other option is to write a facade around both processors, presenting a consistent interface that works with either processor behind the scenes. The only problem with this approach is that as new processors are introduced, you must update the implementation of your facade. It would be very difficult for one individual or organization to keep up with the rapidly changing world of XSLT processors.

But if the facade was an open standard and supported by a large enough user base, the people and organizations that write the XSLT processors would feel pressure to adhere to the common API, rather than the other way around. TrAX was initiated in early 2000 as an effort to define a consistent API to any XSLT processor. Since some of the key people behind TrAX were also responsible for implementing some of the major XSLT processors, it was quickly accepted that TrAX would be a de facto standard, much in the way that SAX is.

Introduction to JAXP 1.1
TrAX was a great idea, and the original work and concepts behind it were absorbed into JAXP Version 1.1. If you search for TrAX on the Web and get the feeling that the effort is waning, this is only because focus has shifted from TrAX to JAXP. Although the name has changed, the concept has not: JAXP provides a standard Java interface to many XSLT processors, allowing you to choose your favorite underlying implementation while retaining portability.

First released in March 2000, Sun's JAXP 1.0 utilized XML 1.0, XML Namespaces 1.0, SAX 1.0, and DOM Level 1. JAXP is a standard extension to Java, meaning that Sun provides a specification through its Java Community Process (JCP) as well as a reference implementation. JAXP 1.1 follows the same basic design philosophies of JAXP 1.0, adding support for DOM Level 2, SAX 2, and XSLT 1.0. A tool like JAXP is necessary because the XSLT specification defines only a transformation language; it says nothing about how to write a Java XSLT processor. Although they all perform the same basic tasks, every processor uses a different API and has its own set of programming conventions.

JAXP is not an XML parser, nor is it an XSLT processor. Instead, it provides a common Java interface that masks differences between various implementations of the supported standards. When using JAXP, your code can avoid dependencies on specific vendor tools, allowing flexibility to upgrade to newer tools when they become available.

The key to JAXP's design is the concept of plugability layers. These layers provide consistent Java interfaces to the underlying SAX, DOM, and XSLT implementations. In order to utilize one of these APIs, you must obtain a factory class without hardcoding Xalan or SAXON code into your application. This is accomplished via a lookup mechanism that relies on Java system properties. Since three separate plugability layers are used, you can use a DOM parser from one vendor, a SAX parser from another vendor, and yet another XSLT processor from someone else. In reality, you will probably need to use a DOM parser compatible with your XSLT processor if you try to transform the DOM tree directly. Figure 5-1 illustrates the high-level architecture of JAXP 1.1.

Figure 5-1. JAXP 1.1 architecture

As shown, application code does not deal directly with specific parser or processor implementations, such as SAXON or Xalan. Instead, you write code against abstract classes that JAXP provides. This level of indirection allows you to pick and choose among different implementations without even recompiling your application.

The main drawback to an API such as JAXP is the "least common denominator" effect, which is all too familiar to AWT programmers. In order to maximize portability, JAXP mostly provides functionality that all XSLT processors support. This means, for instance, that Xalan's custom XPath APIs are not included in JAXP. In order to use value-added features of a particular processor, you must revert to nonportable code, negating the benefits of a plugability layer. Fortunately, most common tasks are supported by JAXP, so reverting to implementation-specific code is the exception, not the rule.

Although the JAXP specification does not define an XML parser or XSLT processor, reference implementations do include these tools. These reference implementations are open source Apache XML tools, (Crimson and Xalan) so complete source code is available.

JAXP 1.1 Implementation
You guessed it...we will now reimplement the simple example using Sun's JAXP 1.1. Behind the scenes, this could use any JAXP 1.1-compliant XSLT processor; this code was developed and tested using Apache's Xalan 2 processor. Example 5-3 contains the complete source code.

--------------------------------------------------------------------------------

Example 5-3: SimpleJaxp.java

package chap5;

import java.io.*;

/**
* A simple demo of JAXP 1.1
*/
public class SimpleJaxp {

/**
   * Accept two command line arguments: the name of
   * an XML file, and the name of an XSLT stylesheet.
   * The result of the transformation
   * is written to stdout.
   */
  public static void main(String[] args)
      throws javax.xml.transform.TransformerException {
    if (args.length != 2) {
      System.err.println("Usage:");
      System.err.println(" java " + SimpleJaxp.class.getName( )
          + " xmlFileName xsltFileName");
      System.exit(1);
    }

File xmlFile = new File(args[0]);
    File xsltFile = new File(args[1]);

javax.xml.transform.Source xmlSource =
        new javax.xml.transform.stream.StreamSource(xmlFile);
    javax.xml.transform.Source xsltSource =
        new javax.xml.transform.stream.StreamSource(xsltFile);
    javax.xml.transform.Result result =
        new javax.xml.transform.stream.StreamResult(System.out);

// create an instance of TransformerFactory
    javax.xml.transform.TransformerFactory transFact =
        javax.xml.transform.TransformerFactory.newInstance( );

javax.xml.transform.Transformer trans =
        transFact.newTransformer(xsltSource);

trans.transform(xmlSource, result);
  }
}