// Utility methods // Any old BeanShell methods - no need for XSLT extensions random = new java.util.Random(); int getNextOrderId() { return random.nextInt(); } // Transformation sub-methods // Split up the transformation, to make things look neater // Just like you split up your Java code // This is called to add a row to the line items table void addLineItems() { addelem { addelem valueOf("itemId/text()") ; addelem valueOf("quantity/text()") ; } } // This is called to add footers with instructions // Shows the use of text() and attribute() functions int i = 1; void addFooter() { String strIndex = String.valueOf(i); i = i + 1; addelem { addelem attribute("type") + ":" + text() ; } } // Main Transformation starts here // First call root to initialize the output root(); // Add a table element for the order table, and one row addelem { addelem { addelem getNextOrderId() ; addelem valueOf("customer/text()") ; } } // Add a table element for the line items table, and add // a row for each line item addelem
{ foreach("lineItems/lineItem") { addLineItems(); } } // add a footer for each instruction // this doesn't make much practical sense, but // addFooter( ) has some features to check out foreach ("//instruction") { addelem
{ addFooter(); } }