”); 70: } 71: 72: public void init(ServletConfig config) throws ServletException { 73: super.init(config); 74: this.config = config; 75: } 76: } The important lines in the servlet in Listing 17.5 are from 18 to 32 On line 18, the servlet reads a request parameter that defines the XML source file and the stylesheet file, which must be defined in the same Web application as the servlet The two filenames are passed as a single parameter called source, each filename being preceded by a forward slash (/) Before looking at the rest of this servlet, a simple HTML form that lets the user invoke this servlet with the example files provided so far is shown in Listing 17.6 17 21 0672323842 CH17 3/20/02 9:32 AM Page 754 754 Day 17 LISTING 17.6 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: Full Text of xsltForm.html XLST Transformations Select an XML document/XSL stylesheet to transform: /job.xml/simple.xsl
You will be able to use this form and servlet to examine most of the example transformations shown today Returning to Listing 17.5, the code in lines 18 to 21 obtains the names for the XML and stylesheet files from the HTTP source parameter Lines 26 and 27 use the getResourceAsStream() method from the ServletContext object to find the named Web application file and return an InputStream object that can be used to read the contents of the file The getResourceAsStream() method hides the real location of the file from the servlet enabling the servlet to be deployed as part of any Web application In Listing 17.5, lines 22 to 32 perform the actual transformation and write the transformed data to the Web page In Lines 33 and 34, the showResource() method returns the text of the original XML data and stylesheet back to the Web browser for the user to view the original XML document, stylesheet, and transformed output on the single page The showResource() method replaces the reserved HTML characters with their replacement string representation To run this demonstration servlet, create a new application called xslt and add the following files to the WAR file: • ApplyXSLT.class • xsltForm.html • jobs.xml • simple.xsl Create a servlet Web application using ApplyXSLT.class as the servlet and give it an alias of /applyXSLT Define the Web application context as /xslt and deploy the application 21 0672323842 CH17 3/20/02 9:32 AM Page 755 Transforming XML Documents 755 You can access the HTML form using the URL http://localhost:8000/xslt/xsltForm.html Select the XML and stylesheet pair you want to view and click the “Submit” button Your screen will look similar to the one shown in Figure 17.1 FIGURE 17.1 Viewing the simple XML transformation 17 To look at the later examples in today’s lesson, you will need to add XML document and stylesheet entries to the list on the HTML form and add the XML document and stylesheet to the xslt Web application So far, you have seen a very simple XLST transformation You will now look in more detail at XSLT and its capabilities XSLT Stylesheets In Listing 17.2, you saw a simple stylesheet that used default transformation rules to remove everything except for the text from an XML document You will now look at how to define your own rules for transforming an XML document Rules are based on matching elements in the XML document and transforming the elements into a new document Text and information from the original XML document can be included or omitted Components from the XML document are matched using the 21 0672323842 CH17 3/20/02 9:32 AM Page 756 756 Day 17 XPath notation defined by the W3C You will learn more about XPath in the “Using XPath with XSLT” section later in today’s lesson, after you have looked at some simple XSLT templates Template Rules The most common XSLT template rules are those for matching and transforming elements The following simple example matches the root node of a document and transforms it into an outline for an HTML document that will be created as you learn more about XSLT’s capabilities Job Details The defines a new template rule in the stylesheet and its match attribute specifies which parts of the XML document will be matched by this rule The root of a document is matched by the forward slash (/); other matching patterns are discussed later in the “Using XPath with XSLT” section The body of the element is output in place of the matched element in the original document In this case, the entire document is replaced by a blank HTML document No other elements in the document will be matched If you want to transform other elements in the original document, you must define additional templates and apply those templates to the body of the matched element The following text adds an element to the rule matching the XML document root: Job Details When this rule is applied to the transformed root element, the body of the root element is scanned for further template matches The output from the other rules is inserted at the point where the element is defined Listing 17.7 shows a simple stylesheet that transforms all of the XML elements into HTML elements 21 0672323842 CH17 3/20/02 9:32 AM Page 757 Transforming XML Documents LISTING 17.7 757 Full Text of basicHTML.xsl 1: 2: 3: 4: 5: Job Details 6: 7: 8: 9: 10: 11: 12: In Listing 17.7, the second rule at lines 9–11 matches every element in the XML document, replaces it with a element, and applies all the templates recursively to the body of the XML element Caution A stylesheet is an XML document, and you must ensure the XML remains valid when outputting HTML In Listing 17.7, on line 10, the text is enclosed inside an HTML paragraph to ensure that the stylesheet remains valid Many authors of HTML simply insert the paragraph tag at the end of the paragraph This will not work with stylesheets because the unterminated tag is not well-formed XML Other HTML tags, such as and , must be treated in a similar manner There are alternative solutions to the problem of defining HTML documents inside XSLT stylesheets that are outside the scope of this chapter Listing 17.8 shows the HTML output from applying the basicHTML.xsl stylesheet to the jobs.xml file shown in Listing 17.3 LISTING 17.8 1: 2: 3: 4: 5: 6: 7: 8: 9: Applying basicHTML.xsl to jobs.xml >java org.apache.xalan.xslt.Process -in XML\job.xml -xsl XSL\basicHTML.xsl Job Details 17 21 0672323842 CH17 3/20/02 9:32 AM Page 758 758 Day 17 LISTING 17.8 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: Continued London Must like to talk and smoke Cigar maker Critic Washington Must be honest Tree surgeon Descriptiom: Location: Skill: Location: London Descriptiom: Must like to talk and smoke Skill: Cigar maker Skill: Critic
Location: Washington
21 0672323842 CH17 3/20/02 9:32 AM Page 761 Transforming XML Documents LISTING 17.10 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 761 ContinuedDescriptiom: Must be honest
Skill: Tree surgeon
Using the tag raises two questions: • What is the text value of an XML element? • What does the select attribute do? These questions are answered in the next two sections Text Representation of XML Elements Every XML node has a textual representation that is used when the tag is defined within a template rule Table 17.1 shows how the textual equivalent of each of the seven XML nodes is obtained TABLE 17.1 Text Values of XML Elements Element Type Description Document root The concatenation of all the text in the document Elements The concatenation of all the text in the body of the element Text The text value of the node, including whitespace Attributes The text value of the attribute, including whitespace Namespaces The namespace URI that is bound to the namespace prefix associated with the node Processing Instructions The text the processing instruction following the target name and including any whitespace Comments The text of the comment between the delimiters As you can see from Table 17.1, every node has a textual equivalent The default rules for a stylesheet only include the text values for the document root, elements, and all text nodes By default, the other four nodes (attributes, namespaces, processing instructions, and comments) are not output Before you can understand the default rules, you will need to study the XPath notation for matching nodes in an XML document 17 21 0672323842 CH17 3/20/02 9:32 AM Page 762 762 Day 17 Using XPath with XSLT XPath is means of identifying nodes within an XML document The W3C identified several aspects of XML that required the ability to identify nodes, for example • Pointers from one XML document to another called XPointer (the equivalent of href in HTML) • Template rules for XSLT stylesheets • Schemas To ensure that the two requirements for identifying nodes share a common syntax, the XPath notation was defined as a separate standard An XPath is a set of patterns that can be used to match nodes within an XML document There are a large number of patterns that can be used to match any part of an XML document Rather than reproduce the entire XPath specification in today’s lesson, you will just study some examples that will help you understand how to use XPath Further information about XPath can be obtained from the WC3 Web site XPath uses the concept of axes and expressions to define a path in the XML document: • Axes define different parts of the XML document structure • Expressions refer to a specific objects within an axis Some of the most frequently used axes have special shortcuts to reduce the amount of typing needed Consider the stylesheet rule you used to match a skill element: Skill: This matches a child “skill” element using a simple abbreviation The full XPath notation for this would be: The axis is child and the expression is an element with the name skill (the double colon separates the axis from the expression) The current node that a path is defined from is called the context node The child axis is used to identify all nodes that are immediate children of the context node Related axes are • self • parent The current node The immediate parent of the context node 21 0672323842 CH17 3/20/02 9:32 AM Page 763 Transforming XML Documents • descendent • descendent-or-self • ancestor 763 Immediate children of the context node, all the children of those nodes, their children, and so on All descendent nodes and the current context node Any node higher up the node tree that contains context node There are several other axes defined in the XPath notation The match=”.” attribute in the example element, shown previously, is another example of a shortcut The full notation is as follows: Skill: The function node() refers to the current context node Additional functions are • name() The name of the context node instead of the body of the node • comment() • text() • processing-instruction() Selects a comment node Selects a text node Selects a processing instruction node Some simple XPath expressions are as follows: • self::comment() All comments in the current element • child::text() • descendent::node() • descendent-or-self::skill All the text nodes in the immediate child nodes All the nodes below the context node All the nodes named skill below the current node, including the current node Expressions can be more complex and specify a node hierarchy: • A skill node that is an immediate child of a job node (in full child::job/child::skill) job/skill XPath expressions can be arbitrarily long and can contain the following special expressions: • The immediate parent node defined as parent::node() • // The current node or any descendent as descendent-or-self::node() • * Any node in the specified axis • | Used to provide alternate patterns (one pattern or another) These patterns can be used to identify any node as illustrated by the following examples: • jobSummary//skill jobSummary node Nodes called skill defined anywhere below the 17 21 0672323842 CH17 3/20/02 9:32 AM Page 764 764 Day 17 • jobSummary/*/skill skill nodes defined as children of children of the jobSummary node • skill/ The immediate parent node of a skill node • location|skill • parent::comment()|child::text() A location or skill node Comment nodes in the immediate parent and text nodes in the immediate child • /|* The document root and all elements Attributes can be selected using the attribute axis, which can be abbreviated to @ For example, • attribute::customer An the attribute called customer of any node (not the node itself) • job/@reference job An attribute called reference so long as it is associated with a node In addition to these basic features, XPath supports a powerful matching language supporting variable-like constructs, expressions, and additional functions Now that you have a basic understanding of Xpath, you can look at the default rules for a stylesheet Default Stylesheet Rules There are some default stylesheet rules that apply to the whole XML document unless overridden by specific template rules The first default rule that ensures all elements are processed is as follows: A second rule is used to output the text of text nodes and attributes: A third rule suppresses comments and processing instructions: If an XML element in the source document matches more than one rule, the most specific rule is applied Consequently, rules defined in a stylesheet will override the default rules 21 0672323842 CH17 3/20/02 9:32 AM Page 765 Transforming XML Documents 765 The second default rule specifies that the text value of attributes should be output, but you can see from Listing 17.4 that the attributes in job.xml (Listing 17.3) have not been included Obviously, there is an extra requirement for processing attributes because this rule has never been invoked Processing Attributes Attributes of XML elements are not processed unless a specific rule is defined to process the element’s attributes An attribute is processed by using the rule selecting one or more attributes The third line in the following rule is the one that applies templates to all attributes: This rule matches all elements and applies templates to the child elements and then that element’s attributes It is the second rule with the select=”@*” attribute that ensures that all attributes are output If you only defined the second rule, no output would be produced because the rule had not been applied to elements in the context node With this extra information, you can now revisit the job.xml file and define a stylesheet that will display the job information in an HTML table Listing 17.11 shows a stylesheet that will convert a element to an HTML table LISTING 17.11 Full Text of table.xsl 1: 2: 3: 4: Job ref: / ➥ 5: 6: 7: 8: 9: 10: 11: 12: 13: 17 ... 675 Security The exact mechanism of mapping principals onto roles is not defined in the EJB specification (at least not yet) There are no tags defined in the DD; instead, each J2EE server defines... study Role Mappings Role User Administrator Group j2ee Applicant applicant Customer customer 675 15 19 0 672 323842 CH15 3/20/02 676 9:33 AM Page 676 Day 15 After defining the role mappings for the... references used in the DD onto principals defined in the target authentication domain Configuring J2EE RI Declarative Web Security Web application roles are defined in deploytool using the Roles