import oracle.xml.parser.v2.*; import java.io.*; import java.net.*; import org.w3c.dom.*; import org.xml.sax.*; /* ** Useful helper routines for making it even easier ** to work with selecting nodes from XML files using ** Oracle's XSLT Engine and XMLNode.selectNodes() Method. */ public class XSLT { public static NodeList select(Document d, String pattern) { return select((XMLDocument)d,pattern); } public static NodeList select(XMLDocument d, String pattern) { try { return ((XMLNode)d).selectNodes(pattern,(NSResolver)d.getDocumentElement()); } catch (XSLException x) { return null; } } public static NodeList select(Element n, String pattern) { return select((XMLElement)n,pattern); } public static NodeList select(XMLElement n, String pattern) { try { return n.selectNodes(pattern,n); } catch (XSLException x) { return null; } } public static String valueOf(Element n, String pattern) { return valueOf( (XMLNode) n, pattern ); } public static String valueOf(Node n, String pattern) { return valueOf( (XMLNode) n, pattern ); } public static String valueOf(XMLElement n, String pattern) { return valueOf( (XMLNode) n, pattern ); } public static String valueOf(XMLNode n, String pattern) { try { NSResolver res = null; if ( n instanceof XMLElement ) res = (NSResolver) n; NodeList nl = n.selectNodes(pattern, res); if (nl != null && nl.getLength() > 0 ) { XMLNode x = (XMLNode) nl.item(0); if ( x instanceof XMLText ) { return x.getNodeValue(); } else if ( x instanceof XMLAttr ) { return ((XMLAttr)x).getNodeValue(); } else if ( x instanceof XMLElement ) { ((XMLElement)x).normalize(); return content((XMLElement)x); } } else { return ""; } } catch (XSLException x) { return ""; } return ""; } private static String content(XMLElement n) { StringBuffer content = new StringBuffer(); XMLNode next = null; NodeList nl = n.getChildNodes(); if (nl != null && nl.getLength() > 0 ) { int j = nl.getLength(); for (int i = 0; i < j; i++ ) { XMLNode nd = (XMLNode) nl.item(i); short nodetype = nd.getNodeType(); if (nodetype == Node.TEXT_NODE) { content.append(nd.getNodeValue()); } else if (nodetype == Node.ELEMENT_NODE) { content.append(content((XMLElement)nd)); } } } return content.toString(); } public static void process( XMLDocument xml, XSLStylesheet xsl, PrintWriter pw ) throws SAXParseException { try { XSLProcessor processor = new XSLProcessor(); DocumentFragment result = processor.processXSL(xsl, (XMLDocument) xml); XMLDocument outDoc = new XMLDocument(); outDoc.appendChild(result); outDoc.print(pw); System.err.flush(); } catch (Exception e) { throw new RuntimeException(e.toString()); } } }