|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.bristle.javalib.xml.XSLUtil
public class XSLUtil
This class contains utility routines for working with XSL.
Usage:
- Some typical scenarios for using this class are:
- To apply an XSL transformation to an XML tree:
xmlXML2 = XSLUtil.transform(xmlXML1, xmlXSL);
- To copy an XML tree:
xml2 = XSLUtil.copy(xml1);
- To strip the comments from an XML tree:
xml2 = XSLUtil.stripComments(xml1);
- To copy an XML tree into a subtree of another XML tree:
XSLUtil.copy(xml1, xmlChildOfXML2);
- To sort the child Elements (in this case, the B Elements) within an
XML Element (the A Element), ordering them by the value of a specified
XML Element (the C Element) nested within each child Element.
Document dom = XMLUtil.loadDocumentFromString
("" +
"" +
"3 " +
"" +
"" +
"2 " +
"" +
"" +
"10 " +
"" +
"");
XSLUtil.sortElements(dom, "A", "C",
XSLUtil.SortDataType.TEXT,
XSLUtil.SortOrder.ASCENDING,
XSLUtil.SortCaseOrder.UPPER_CASE_FIRST);
Resulting DOM:
10
2
3
- You can also sort numerically, instead of alphabetically.
To sort the child Elements (in this case, the B Elements) within an
XML Element (the A Element), ordering them by the numeric value of a
specified XML Element (the C Element) nested within each child Element.
Document dom = XMLUtil.loadDocumentFromString
("" +
"" +
"3 " +
"" +
"" +
"2 " +
"" +
"" +
"10 " +
"" +
"");
XSLUtil.sortElements(dom, "A", "C",
XSLUtil.SortDataType.NUMBER,
XSLUtil.SortOrder.ASCENDING,
XSLUtil.SortCaseOrder.UPPER_CASE_FIRST);
Resulting DOM:
2
3
10
- You can sort by attribute value, instead of by nested Element value.
To sort the child Elements (in this case, the B Elements) within an
XML Element (the A Element), ordering them by the value of a
specified XML Attribute (the attr1 Attribute) of each child Element.
Document dom = XMLUtil.loadDocumentFromString
("" +
"" +
"some text " +
"" +
"" +
"some more text " +
"" +
"" +
"yet more text " +
"" +
"");
XSLUtil.sortElements(dom, "A", "@attr1",
XSLUtil.SortDataType.TEXT,
XSLUtil.SortOrder.ASCENDING,
XSLUtil.SortCaseOrder.UPPER_CASE_FIRST);
Resulting DOM:
yet more text
some more text
some text
- The sorted child Elements need not all be the same type of Element.
To sort the child Elements (in this case, the B and Z Elements)
within an XML Element (the A Element), ordering them by the value of
a specified XML Element (the C Element) nested within each child
Element.
Document dom = XMLUtil.loadDocumentFromString
("" +
"" +
"3 " +
"" +
"" +
"2 " +
" " +
"" +
"10 " +
"" +
"");
XSLUtil.sortElements(dom, "A", "C",
XSLUtil.SortDataType.TEXT,
XSLUtil.SortOrder.ASCENDING,
XSLUtil.SortCaseOrder.UPPER_CASE_FIRST);
Resulting DOM:
10
2
3
- You can also sort the children of multiple parents at the same time.
In the above examples, if there had been multiple A Elements in the
XML tree (even at different nesting levels), each would have had its
child Elements sorted within it. In such a case, you can specify a
more specific parent XPath, like "A[2]" to limit the sort to a
single parent.
- You can use any valid XPath to specify the parents, not just the
simplistic "A" and "A[2]" examples above.
- You can use any valid XPath to specify the sort key, not just the
simplistic "C" and "@attr1" examples above.
- See the source code of the inner Tester class for more examples.
Assumptions:
Effects:
- None.
Anticipated Changes:
Notes:
- Pay particular attention to the distinction between the different
XML DOM node types. The names and comments in this class use these
different terms very precisely:
Document An entire XML DOM document
DocumentFragment
A fragment of an XML document.
Element An XML element or tag (an item in angle brackets)
Text A sequence of text stored in an Element
Node Could be any of the above, or an XML attribute, CDATA,
comment, document type, entity, etc.
See the org.w3c.dom.Node documentation for details.
Implementation Notes:
Portability Issues:
Revision History:
$Log$
| Nested Class Summary | |
|---|---|
static class |
XSLUtil.SortCaseOrder
This class represents an enumerated type that identifies sort case orders (upper case first, lower case first, case insensitive). |
static class |
XSLUtil.SortDataType
This class represents an enumerated type that identifies data types to sort by. |
static class |
XSLUtil.SortOrder
This class represents an enumerated type that identifies sort orders (ascending and descending). |
static class |
XSLUtil.Tester
Each class contains a Tester inner class with a main() for easier unit testing. |
| Constructor Summary | |
|---|---|
XSLUtil()
|
|
| Method Summary | |
|---|---|
static Document |
copy(Node xml)
Uses an XSL transformation to copy the specified XML Document, DocumentFragment or Element to a new Document. |
static Node |
copy(Node xmlXML,
DOMResult xmlResult)
Uses an XSL transformation to copy the specified XML Document, DocumentFragment or Element to the specified DOMResult (which should refer to an XML Document, DocumentFragment or Element), and returning the generated XML as a Node. |
static Node |
copy(Node xmlXML,
Node xmlResult)
Uses an XSL transformation to copy the specified XML Document, DocumentFragment or Element to the specified Node (which should be an XML Document, DocumentFragment or Element), and returning the generated XML as a Node. |
static Document |
getIdentityTransformationStylesheetDocument()
Returns the identity transformation stylesheet as an XML Document. |
static String |
getIdentityTransformationStylesheetString()
Returns the identity transformation stylesheet as a string of XSL. |
static String |
getIdentityTransformationTemplateString()
Returns the identity transformation template as a string of XSL. |
static Document |
getSortElementsTransformationStylesheetDocument(String strParentElementXPath,
String strSortKeyXPath,
XSLUtil.SortDataType dataType,
XSLUtil.SortOrder order,
XSLUtil.SortCaseOrder caseOrder)
Returns a sort transformation stylesheet as an XML Document. |
static String |
getSortElementsTransformationStylesheetString(String strParentElementXPath,
String strSortKeyXPath,
XSLUtil.SortDataType dataType,
XSLUtil.SortOrder order,
XSLUtil.SortCaseOrder caseOrder)
Returns a sort transformation stylesheet as a string of XSL. |
static String |
getSortElementsTransformationTemplateString(String strParentElementXPath,
String strSortKeyXPath,
XSLUtil.SortDataType dataType,
XSLUtil.SortOrder order,
XSLUtil.SortCaseOrder caseOrder)
Returns a sortElements transformation template as a string of XSL. |
static Document |
getStripCommentsTransformationStylesheetDocument()
Returns a "strip comments" transformation stylesheet as an XML Document. |
static String |
getStripCommentsTransformationStylesheetString()
Returns a "strip comments" transformation stylesheet as a string of XSL. |
static String |
getStripCommentsTransformationTemplateString()
Returns a "strip comments" transformation template as a string of XSL. |
static Node |
sortElements(Node xml,
String strParentElementXPath,
String strSortKeyXPath,
XSLUtil.SortDataType dataType,
XSLUtil.SortOrder order,
XSLUtil.SortCaseOrder caseOrder)
Sorts the child Elements of the specified Elements of the specified XML Document, DocumentFragment or Element, ordering them by the value of the specified XML Element nested within each child Element, leaving the original XML unmodified, copying the sorted XML to a new Document, and returning the sorted XML as a Node. |
static Node |
sortElements(Node xmlXML,
String strParentElementXPath,
String strSortKeyXPath,
XSLUtil.SortDataType dataType,
XSLUtil.SortOrder order,
XSLUtil.SortCaseOrder caseOrder,
DOMResult xmlResult)
Sorts the child Elements of the specified Elements of the specified XML Document, DocumentFragment or Element, ordering them by the value of the specified XML Element nested within each child Element, leaving the original XML unmodified, copying the sorted XML to the specified DOMResult (which should refer to an XML Document, DocumentFragment or Element), and returning the sorted XML as a Node. |
static Node |
sortElements(Node xmlXML,
String strParentElementXPath,
String strSortKeyXPath,
XSLUtil.SortDataType dataType,
XSLUtil.SortOrder order,
XSLUtil.SortCaseOrder caseOrder,
Node xmlResult)
Sorts the child Elements of the specified Elements of the specified XML Document, DocumentFragment or Element, ordering them by the value of the specified XML Element nested within each child Element, leaving the original XML unmodified, copying the sorted XML to the specified Node (which should be an XML Document, DocumentFragment or Element), and returning the sorted XML as a Node. |
static Document |
stripComments(Node xml)
Uses an XSL transformation to copy the specified XML Document, DocumentFragment or Element to a new Document, but stripping the comments out of the generated copy. |
static Node |
stripComments(Node xmlXML,
DOMResult xmlResult)
Uses an XSL transformation to copy the specified XML Document, DocumentFragment or Element to the specified DOMResult (which should refer to an XML Document, DocumentFragment or Element), but stripping the comments out of the generated copy, and returning the generated XML as a Node. |
static Node |
stripComments(Node xmlXML,
Node xmlResult)
Uses an XSL transformation to copy the specified XML Document, DocumentFragment or Element to the specified Node (which should be an XML Document, DocumentFragment or Element), but stripping the comments out of the generated copy, and returning the generated XML as a Node. |
static Document |
transform(Node xmlXML,
Node xmlXSL)
Applies the specified XSL transformation to the specified XML Document, DocumentFragment or Element, leaving the original XML untouched, and returning a new Document. |
static Node |
transform(Node xmlXML,
Node xmlXSL,
DOMResult xmlResult)
Applies the specified XSL transformation to the specified XML Document, DocumentFragment or Element, leaving the original XML untouched, storing the generated XML in the specified DOMResult (which should refer to an XML Document, DocumentFragment or Element), and returning the generated XML as a Node. |
static Node |
transform(Node xmlXML,
Node xmlXSL,
Node xmlResult)
Applies the specified XSL transformation to the specified XML Document, DocumentFragment or Element, leaving the original XML untouched, storing the generated XML in the specified result Node (which should refer to an XML Document, DocumentFragment or Element), and returning the generated XML as a Node. |
static String |
wrapInXSLStylesheet(String strXSL)
Returns an XSL stylesheet as a String, wrapping the syntax for the required xsl:stylesheet node around the specified XML string which may be one or more xsl:templates, or any other valid XSL syntax. |
| Methods inherited from class java.lang.Object |
|---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public XSLUtil()
| Method Detail |
|---|
public static String wrapInXSLStylesheet(String strXSL)
public static String getIdentityTransformationTemplateString()
wrapInXSLStylesheet(java.lang.String)public static String getIdentityTransformationStylesheetString()
public static Document getIdentityTransformationStylesheetDocument()
throws SAXException,
IOException
SAXException - Should never happen, unless there is an error in
this class.
IOException - Should never happen, unless there is an error in
this class.public static String getStripCommentsTransformationTemplateString()
wrapInXSLStylesheet(java.lang.String)public static String getStripCommentsTransformationStylesheetString()
public static Document getStripCommentsTransformationStylesheetDocument()
throws SAXException,
IOException
SAXException - Should never happen, unless there is an error in
this class.
IOException - Should never happen, unless there is an error in
this class.
public static String getSortElementsTransformationTemplateString(String strParentElementXPath,
String strSortKeyXPath,
XSLUtil.SortDataType dataType,
XSLUtil.SortOrder order,
XSLUtil.SortCaseOrder caseOrder)
strParentElementXPath - XPath of parent Elements whose child Elements
are to be sortedstrSortKeyXPath - XPath of XML Node to be used as the sort key
within each child Element to be sorted.dataType - Data type of the sort keyorder - Order of the sortcaseOrder - Effect of case on the sort order
wrapInXSLStylesheet(java.lang.String)
public static String getSortElementsTransformationStylesheetString(String strParentElementXPath,
String strSortKeyXPath,
XSLUtil.SortDataType dataType,
XSLUtil.SortOrder order,
XSLUtil.SortCaseOrder caseOrder)
strParentElementXPath - XPath of parent Elements whose child Elements
are to be sortedstrSortKeyXPath - XPath of XML Node to be used as the sort key
within each Element to be sorted.dataType - Data type of the sort keyorder - Order of the sortcaseOrder - Effect of case on the sort order
public static Document getSortElementsTransformationStylesheetDocument(String strParentElementXPath,
String strSortKeyXPath,
XSLUtil.SortDataType dataType,
XSLUtil.SortOrder order,
XSLUtil.SortCaseOrder caseOrder)
throws SAXException,
IOException
strParentElementXPath - XPath of parent Elements whose child Elements
are to be sortedstrSortKeyXPath - XPath of XML Node to be used as the sort key
within each Element to be sorted.dataType - Data type of the sort keyorder - Order of the sortcaseOrder - Effect of case on the sort order
SAXException - Should never happen, unless there is an error in
this class.
IOException - Should never happen, unless there is an error in
this class.
public static Node transform(Node xmlXML,
Node xmlXSL,
DOMResult xmlResult)
throws TransformerConfigurationException,
TransformerException
xmlXML - XML to be transformedxmlXSL - XSL transformationxmlResult - The XML Document, DocumentFragment, or Element in
which to store the generated XML.
TransformerConfigurationException - When the Transformer cannot be constructed.
TransformerException - When the transformation fails.
public static Node transform(Node xmlXML,
Node xmlXSL,
Node xmlResult)
throws TransformerConfigurationException,
TransformerException
xmlXML - XML to be transformedxmlXSL - XSL transformationxmlResult - The XML Document, DocumentFragment, or Element in
which to store the generated XML.
TransformerConfigurationException - When the Transformer cannot be constructed.
TransformerException - When the transformation fails.
public static Document transform(Node xmlXML,
Node xmlXSL)
throws TransformerConfigurationException,
TransformerException
xmlXML - XML to be transformedxmlXSL - XSL transformation
TransformerConfigurationException - When the Transformer cannot be constructed.
TransformerException - When the transformation fails.
public static Node copy(Node xmlXML,
DOMResult xmlResult)
throws SAXException,
IOException,
TransformerConfigurationException,
TransformerException
xmlXML - XML to be transformedxmlResult - The XML Document, DocumentFragment, or Element in
which to store the generated XML.
SAXException - Should never happen, unless there is an error in
this class.
IOException - Should never happen, unless there is an error in
this class.
TransformerConfigurationException - When the Transformer cannot be constructed.
TransformerException - When the transformation fails.
public static Node copy(Node xmlXML,
Node xmlResult)
throws SAXException,
IOException,
TransformerConfigurationException,
TransformerException
xmlXML - XML to be transformedxmlResult - The XML Document, DocumentFragment, or Element in
which to store the generated XML.
SAXException - Should never happen, unless there is an error in
this class.
IOException - Should never happen, unless there is an error in
this class.
TransformerConfigurationException - When the Transformer cannot be constructed.
TransformerException - When the transformation fails.
public static Document copy(Node xml)
throws SAXException,
IOException,
TransformerConfigurationException,
TransformerException
xml - XML to be transformed
SAXException - Should never happen, unless there is an error in
this class.
IOException - Should never happen, unless there is an error in
this class.
TransformerConfigurationException - When the Transformer cannot be constructed.
TransformerException - When the transformation fails.
public static Node stripComments(Node xmlXML,
DOMResult xmlResult)
throws SAXException,
IOException,
TransformerConfigurationException,
TransformerException
xmlXML - XML to be transformedxmlResult - The XML Document, DocumentFragment, or Element in
which to store the generated XML.
SAXException - Should never happen, unless there is an error in
this class.
IOException - Should never happen, unless there is an error in
this class.
TransformerConfigurationException - When the Transformer cannot be constructed.
TransformerException - When the transformation fails.
public static Node stripComments(Node xmlXML,
Node xmlResult)
throws SAXException,
IOException,
TransformerConfigurationException,
TransformerException
xmlXML - XML to be transformedxmlResult - The XML Document, DocumentFragment, or Element in
which to store the generated XML.
SAXException - Should never happen, unless there is an error in
this class.
IOException - Should never happen, unless there is an error in
this class.
TransformerConfigurationException - When the Transformer cannot be constructed.
TransformerException - When the transformation fails.
public static Document stripComments(Node xml)
throws SAXException,
IOException,
TransformerConfigurationException,
TransformerException
xml - XML to be transformed
SAXException - Should never happen, unless there is an error in
this class.
IOException - Should never happen, unless there is an error in
this class.
TransformerConfigurationException - When the Transformer cannot be constructed.
TransformerException - When the transformation fails.
public static Node sortElements(Node xmlXML,
String strParentElementXPath,
String strSortKeyXPath,
XSLUtil.SortDataType dataType,
XSLUtil.SortOrder order,
XSLUtil.SortCaseOrder caseOrder,
DOMResult xmlResult)
throws SAXException,
IOException,
TransformerConfigurationException,
TransformerException
xmlXML - XML to be sortedstrParentElementXPath - XPath of parent Elements whose child Elements
are to be sortedstrSortKeyXPath - XPath of XML Node to be used as the sort key within
each child Element to be sorted.dataType - Data type of the sort keyorder - Order of the sortcaseOrder - Effect of case on the sort orderxmlResult - The XML Document, DocumentFragment, or Element in
which to store the generated XML.
SAXException - Should never happen, unless there is an error in
this class.
IOException - Should never happen, unless there is an error in
this class.
TransformerConfigurationException - When the Transformer cannot be constructed.
TransformerException - When the transformation fails.
public static Node sortElements(Node xmlXML,
String strParentElementXPath,
String strSortKeyXPath,
XSLUtil.SortDataType dataType,
XSLUtil.SortOrder order,
XSLUtil.SortCaseOrder caseOrder,
Node xmlResult)
throws SAXException,
IOException,
TransformerConfigurationException,
TransformerException
xmlXML - XML to be sortedstrParentElementXPath - XPath of parent Elements whose child Elements
are to be sortedstrSortKeyXPath - XPath of XML Node to be used as the sort key within
each child Element to be sorted.dataType - Data type of the sort keyorder - Order of the sortcaseOrder - Effect of case on the sort orderxmlResult - The XML Document, DocumentFragment, or Element in
which to store the generated XML.
SAXException - Should never happen, unless there is an error in
this class.
IOException - Should never happen, unless there is an error in
this class.
TransformerConfigurationException - When the Transformer cannot be constructed.
TransformerException - When the transformation fails.
public static Node sortElements(Node xml,
String strParentElementXPath,
String strSortKeyXPath,
XSLUtil.SortDataType dataType,
XSLUtil.SortOrder order,
XSLUtil.SortCaseOrder caseOrder)
throws SAXException,
IOException,
TransformerConfigurationException,
TransformerException
xml - XML to be sortedstrParentElementXPath - XPath of parent Elements whose child Elements
are to be sortedstrSortKeyXPath - XPath of XML Node to be used as the sort key within
each child Element to be sorted.dataType - Data type of the sort keyorder - Order of the sortcaseOrder - Effect of case on the sort order
SAXException - Should never happen, unless there is an error in
this class.
IOException - Should never happen, unless there is an error in
this class.
TransformerConfigurationException - When the Transformer cannot be constructed.
TransformerException - When the transformation fails.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||