The following example shows how to apply an XSL Transformation to a JDOM document and get the transformation result in the form of a list of JDOM nodes:
public static List transform(Document doc, String stylesheet)
throws JDOMException {
try {
Transformer transformer = TransformerFactory.newInstance()
.newTransformer(new StreamSource(stylesheet));
JDOMSource in = new JDOMSource(doc);
JDOMResult out = new JDOMResult();
transformer.transform(in, out);
return out.getResult();
}
catch (TransformerException e) {
throw new JDOMException("XSLT Transformation failed", e);
}
}
org.jdom2.transform.JDOMSource
| Modifier and Type | Class and Description |
|---|---|
| private class | |
| private static class |
| Modifier and Type | Field and Description |
|---|---|
| private JDOMFactory | factory
The custom JDOM factory to use when building the transformation
result or |
| public static final String | JDOM_FEATURE
If |
| private boolean | queried
Whether the application queried the result (as a list or a document) since it was last set. |
| private Document | |
| private List | resultlist
The result of a transformation, as set by Transformer implementations that natively support JDOM, as a JDOM document or a list of JDOM nodes. |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public Document | Returns: the transformation result as a JDOM document ornull if the result of the transformation
can not be converted into a well-formed document.Returns the result of an XSL Transformation as a JDOM document. |
| public JDOMFactory | Returns: the customJDOMFactory used to build the
transformation result or null if the
default JDOM classes are being used.Returns the custom JDOMFactory used to build the transformation result. |
| public List | Returns: the transformation result as a (possibly empty) list of JDOM nodes (Elements, Texts, Comments, PIs...).Returns the result of an XSL Transformation as a list of JDOM nodes. |
| private void | retrieveResult()
Checks whether a transformation result has been set and, if not, retrieves the result tree being built by the document builder. |
| public void | setDocument(Document
the JDOM document result of a transformation. document)Sets the document produced as result of an XSL Transformation. |
| public void | setFactory(JDOMFactory
the custom factory)JDOMFactory to use or
null to use the default JDOM
classes.Sets a custom JDOMFactory to use when building the transformation result. |
| public void | setHandler(ContentHandler
Must be a non-null ContentHandler reference. handler)Overrides javax. |
| public void | setLexicalHandler(LexicalHandler
A non-null LexicalHandler for
handling lexical parse events. handler)Overrides javax. |
| public void |
| factory | back to summary |
|---|---|
| private JDOMFactory factory The custom JDOM factory to use when building the transformation
result or | |
| JDOM_FEATURE | back to summary |
|---|---|
| public static final String JDOM_FEATURE If Note This implementation does not override
the | |
| queried | back to summary |
|---|---|
| private boolean queried Whether the application queried the result (as a list or a document) since it was last set. | |
| resultdoc | back to summary |
|---|---|
| private Document resultdoc | |
| resultlist | back to summary |
|---|---|
| private List<Content> resultlist The result of a transformation, as set by Transformer implementations that natively support JDOM, as a JDOM document or a list of JDOM nodes. | |
| JDOMResult | back to summary |
|---|---|
| public JDOMResult() Public default constructor. | |
| getDocument | back to summary |
|---|---|
| public Document getDocument() Returns the result of an XSL Transformation as a JDOM document.
If the result of the transformation is a list of nodes,
this method attempts to convert it into a JDOM document. If
successful, any subsequent call to Warning The XSLT 1.0 specification states that
the output of an XSL transformation is not a well-formed XML
document but a list of nodes. Applications should thus use
| |
| getFactory | back to summary |
|---|---|
| public JDOMFactory getFactory() Returns the custom JDOMFactory used to build the transformation result.
| |
| getResult | back to summary |
|---|---|
| public List Returns the result of an XSL Transformation as a list of JDOM nodes.
If the result of the transformation is a JDOM document,
this method converts it into a list of JDOM nodes; any
subsequent call to | |
| retrieveResult | back to summary |
|---|---|
| private void retrieveResult() Checks whether a transformation result has been set and, if not, retrieves the result tree being built by the document builder. | |
| setDocument | back to summary |
|---|---|
| public void setDocument(Document document) Sets the document produced as result of an XSL Transformation. Note This method shall be used by the
| |
| setFactory | back to summary |
|---|---|
| public void setFactory(JDOMFactory factory) Sets a custom JDOMFactory to use when building the transformation result. Use a custom factory to build the tree with your own subclasses of the JDOM classes.
| |
| setHandler | back to summary |
|---|---|
| public void setHandler(ContentHandler handler) Overrides javax. Sets the target to be a SAX2 ContentHandler.
| |
| setLexicalHandler | back to summary |
|---|---|
| public void setLexicalHandler(LexicalHandler handler) Overrides javax. Sets the SAX2 LexicalHandler for the output. This is needed to handle XML comments and the like. If the lexical handler is not set, an attempt should be made by the transformer to cast the ContentHandler to a LexicalHandler.
| |
| setResult | back to summary |
|---|---|
| public void setResult(List<Content> result) Sets the object(s) produced as result of an XSL Transformation. Note This method shall be used by the
| |
| Modifier and Type | Field and Description |
|---|---|
| private JDOMResult. | saxHandler
The actual JDOM document builder. |
| private boolean | startDocumentReceived
Whether the startDocument event was received. |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public void | characters(char[]
An array of characters. ch, int The starting position in the array. start, int The number of characters to use from the array. length)Overrides org. Implements org. |
| public void | comment(char[]
an array holding the characters in the comment. ch, int the starting position in the array. start, int the number of characters to use from the array. length)Implements org. |
| public void | endCDATA()
Implements org. |
| public void | endDTD()
Implements org. |
| public void | endEntity(String
the name of the entity that is ending. name)Implements org. |
| private void | |
| public List | Returns: the transformation result as a (possibly empty) list of JDOM nodes (Elements, Texts, Comments, PIs...) ornull if no new transformation occurred
since the result of the previous one was returned.Returns the result of an XSL Transformation. |
| public void | ignorableWhitespace(char[]
An array of characters. ch, int The starting position in the array. start, int The number of characters to use from the array. length)Overrides org. Implements org. |
| public void | processingInstruction(String
The processing instruction target. target, String The text following the target. data)Overrides org. Implements org. |
| public void | skippedEntity(String
The name of the skipped entity. name)Overrides org. Implements org. |
| public void | startCDATA()
Implements org. |
| public void | startDocument()
Overrides org. Implements org. |
| public void | startDTD(String
the document type name. name, String the declared public identifier for the
external DTD subset, or publicId, String null
if none was declared.the declared system identifier for the
external DTD subset, or systemId)null
if none was declared.Implements org. |
| public void | startElement(String
the Namespace URI, or the empty string if
the element has no Namespace URI or if
Namespace processing is not being performed. nsURI, String the local name (without prefix), or the
empty string if Namespace processing is
not being performed. localName, String the qualified name (with prefix), or the
empty string if qualified names are not
available. qName, Attributes The attributes attached to the element. If
there are no attributes, it shall be an
empty Attributes object. atts)Overrides org. Implements org. |
| public void | startEntity(String
the name of the entity. If it is a parameter
entity, the name will begin with '%', and if it
is the external DTD subset, it will be "[dtd]". name)Implements org. |
| public void | startPrefixMapping(String
The Namespace prefix. prefix, String The Namespace URI. uri)Overrides org. Implements org. |
| saxHandler | back to summary |
|---|---|
| private JDOMResult. The actual JDOM document builder. | |
| startDocumentReceived | back to summary |
|---|---|
| private boolean startDocumentReceived Whether the startDocument event was received. Some XSLT processors such as Oracle's do not fire this event. | |
| DocumentBuilder | back to summary |
|---|---|
| public DocumentBuilder() Public default constructor. | |
| characters | back to summary |
|---|---|
| public void characters(char[] ch, int start, int length) throws SAXException Overrides org. Implements org. [SAX ContentHandler interface support] Receives notification of character data.
| |
| comment | back to summary |
|---|---|
| public void comment(char[] ch, int start, int length) throws SAXException Implements org. [SAX LexicalHandler interface support] Reports an XML comment anywhere in the document.
| |
| endCDATA | back to summary |
|---|---|
| public void endCDATA() throws SAXException Implements org. [SAX LexicalHandler interface support] Reports the end of a CDATA section.
| |
| endDTD | back to summary |
|---|---|
| public void endDTD() throws SAXException Implements org. [SAX LexicalHandler interface support] Reports the end of DTD declarations.
| |
| endEntity | back to summary |
|---|---|
| public void endEntity(String name) throws SAXException Implements org. [SAX LexicalHandler interface support] Reports the end of an entity.
| |
| ensureInitialization | back to summary |
|---|---|
| private void ensureInitialization() throws SAXException | |
| getResult | back to summary |
|---|---|
| public List Returns the result of an XSL Transformation. | |
| ignorableWhitespace | back to summary |
|---|---|
| public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException Overrides org. Implements org. [SAX ContentHandler interface support] Receives notification of ignorable whitespace in element content.
| |
| processingInstruction | back to summary |
|---|---|
| public void processingInstruction(String target, String data) throws SAXException Overrides org. Implements org. [SAX ContentHandler interface support] Receives notification of a processing instruction.
| |
| skippedEntity | back to summary |
|---|---|
| public void skippedEntity(String name) throws SAXException Overrides org. Implements org. [SAX ContentHandler interface support] Receives notification of a skipped entity.
| |
| startCDATA | back to summary |
|---|---|
| public void startCDATA() throws SAXException Implements org. [SAX LexicalHandler interface support] Reports the start of a CDATA section.
| |
| startDocument | back to summary |
|---|---|
| public void startDocument() throws SAXException Overrides org. Implements org. [SAX ContentHandler interface support] Processes a start of document event. This implementation creates a new JDOM document builder and marks the current result as "under construction".
| |
| startDTD | back to summary |
|---|---|
| public void startDTD(String name, String publicId, String systemId) throws SAXException Implements org. [SAX LexicalHandler interface support] Reports the start of DTD declarations, if any.
| |
| startElement | back to summary |
|---|---|
| public void startElement(String nsURI, String localName, String qName, Attributes atts) throws SAXException Overrides org. Implements org. [SAX ContentHandler interface support] Receives notification of the beginning of an element. This implementation ensures that startDocument() has been called prior processing an element.
| |
| startEntity | back to summary |
|---|---|
| public void startEntity(String name) throws SAXException Implements org. [SAX LexicalHandler interface support] Reports the beginning of some internal and external XML entities.
| |
| startPrefixMapping | back to summary |
|---|---|
| public void startPrefixMapping(String prefix, String uri) throws SAXException Overrides org. Implements org. [SAX ContentHandler interface support] Begins the scope of a prefix-URI Namespace mapping.
| |
| Modifier and Type | Field and Description |
|---|---|
| private Element | dummyRoot
A dummy root element required by SAXHandler that can only cope with well-formed documents. |
| Access | Constructor and Description |
|---|---|
| public | FragmentHandler(JDOMFactory
The Factory to use to create content instances factory)Public constructor. |
| Modifier and Type | Method and Description |
|---|---|
| private List | Returns: a (possibly empty) list of JDOM nodes, detached from their parent.the element to get the content from. elt)Returns the content of a JDOM Element detached from it. |
| public List | Returns: the transformation result as a (possibly empty) list of JDOM nodes (Elements, Texts, Comments, PIs...).Returns the result of an XSL Transformation. |
| dummyRoot | back to summary |
|---|---|
| private Element dummyRoot A dummy root element required by SAXHandler that can only cope with well-formed documents. | |
| FragmentHandler | back to summary |
|---|---|
| public FragmentHandler(JDOMFactory factory) Public constructor.
| |
| getDetachedContent | back to summary |
|---|---|
| private List Returns the content of a JDOM Element detached from it. | |
| getResult | back to summary |
|---|---|
| public List Returns the result of an XSL Transformation. | |