Session 7 - Main Theme XML Information Rendering (Part I)
Total Page:16
File Type:pdf, Size:1020Kb
XML for Java Developers G22.3033-002 Session 7 - Main Theme XML Information Rendering (Part I) Dr. Jean-Claude Franchitti New York University Computer Science Department Courant Institute of Mathematical Sciences 1 Agenda Summary of Previous Session Extensible Stylesheet Language Transformation (XSL-T) Extensible Stylesheet Language Formatting Object (XSL-FO) XML and Document/Content Management Introduction to XML Application Servers Working with XSLT-T and XSL-FO Processors Assignment 4a+4b (due in two week) 2 1 Summary of Previous Session Summary of Previous Session Document Object Model (DOM) Advanced XML Parser Technology JDOM: Java-Centric API for XML JAXP: Java API for XML Processing Parsers comparison Latest W3C APIs and Standards for Processing XML XML Infoset, DOM Level 3, Canonical XML XML Signatures, XBase, XInclude XML Schema Adjuncts Java-Based XML Data Processing Frameworks Assignment #3 3 XML-Based Rendering Development XML Software Development Methodology Language + Stepwise Process + Tools Rational Unified Process (RUP) vs. “XML Unified Process” XML Application Development Infrastructure Metadata Management (e.g., XMI) XSLT, XPath XSL-FO APIs (JAXP, JAXB, JDOM, SAX, DOM) XML Tools (e.g., XML Editors, Apache’s FOP, Antenna House’s XSL Formatter, HTML/CSS1/2/3, XHTML, XForms, WCAG XML App. Components Involved in the Rendering Phase: Application(s) of XML XML-based applications/services (markup language mediators) MOM, POP, Other Services (e.g., persistence) 4 Application Infrastructure Frameworks 2 Part I Introduction to XSL 5 EXtensible Stylesheet (XSL) Language Family Family of recommendations to define XML document transformation and presentation as “style sheets” Three parts: A language for transforming XML documents XSL Transformations (XSLT) Expression language used by XSLT to access or refer to parts of an XML document XML Path Language (XPath) (also used for XML Linking) A XML vocabulary for specifying formatting semantics XSL Formatting Objects (XSL-FO) See http://www.w3.org/Style/XSL for the XSLT 1.0, XPath 1.0, and XSL-FO 1.0 recommendations and working drafts of XSLT 2.0 and XPath 2.0 6 3 How Does XSL Work? An XSL style sheet is an XML document XSLT elements in an XSL style sheet correspond to a series of XSL “transformation” rules (i.e., XML tree transformation and/or formatting rules) XSLT rules describe how particular XML tags are to be converted to “flow objects” as the document is read XPath (e.g., para[@type="warning"][5]) http://www.w3.org/2002/11/xquery-xpath-applets/xpathApplet.html 7 XSL Example XML Document <scene> <FX>General Road Building noises.</FX> <speech speaker="Prosser"> Come off it Mr Dent, you can't win you know. There's no point in lying down in the path of progress. </speech> <speech speaker="Arthur"> I've gone off the idea of progress. It's overrated </speech> </scene> 8 4 XSL Example (continued) Sample stylesheet <xsl:template match="FX"> <fo:block font-weight="bold"> <xsl:apply-templates/> </fo:block> </xsl:template> <xsl:template match="speech[@speaker='Arthur']"> <fo:block background-color="blue"> <xsl:value-of select="@speaker"/>: <xsl:apply-templates/> </fo:block> </xsl:template> 9 XML Data Rendering Patterns Manipulating and Rendering XML Structures Using Java XSLT Transform Sort Output XSLT + XSL-FO Format Output Querying will be covered separately 10 5 XSL Related Technologies DSSSL & DSSSL-O CSS 1, 2, 3 … http://www.w3.org/Style/CSS/ XSLT XPath XSL-FO XSLT Processors Stylus Studio XSL development environment IBM XSL Editor Saxon and Xalan XSLT processors XSL-FO Processors Antenna House 11 fop XSL Processing See http://www.w3.org/Style/XSL/ Processing Alternatives: HTML + CSS -> Presentation XML + CSS -> Presentation XML + XSLT -> XSL-FO -> Presentation XML + XSLT -> XML/HTML + CSS -> Presentation Client or Server Processing ? See Session 2 handout on IE5’s implementation of the XSL Spec. Examples See Session 2 Sub-Topic 1 Presentation: Beginning XML See Session 2 handouts on XSL Tree Transformation Language See Session 2 handout on Cascading Stylesheets See Session 2 handout on Styling Documents Using XSL 12 6 A Language for “Mapping XML” (LMX) LMX is a sample textbook application st XML and Java texbook (1 edition) LMX can convert a document in one DTD into another DTD and vice versa LMX uses rules to describe bi-directional “MOM” conversions between two sets of documents Rules have a “from-pattern” and a “to-pattern”b to respectively match the source document, and construct the target document Some restrictions exist w.r.t. the LMX patterns in order to simplify the program as much as possible LMX can also be used to convert a XML document to HTML (“POP” application) 13 How Does the LMX Processor Work? LMX makes heavy use of the DOM 1.0 API LMX uses XML4J internally to: Parse a rule file Parse a source document Generate a target document See chapter 4.3 in the XML and Java textbook (1st Edition) for a detailed description of the LMX implementation 14 7 LMX v.s. the eXtensible Stylesheet Language (XSL) LMX and XSL both provide a syntax to encode “Style Sheets” Each XML document can be associated with a style sheet that describes how elements should be organized and formatted for presentation XSL style sheets provide custom appearances that give a web site a unified look and feel 15 Part II Extensible Stylesheet Language Transformation (XSLT) 16 8 XSLT http://www.dpawson.co.uk/xsl/sect1/sect1.html An XSLT stylesheet specifies the presentation of a class of XML documents Describes how an instance of the class is transformed into an XML document that uses a formatting vocabulary (e.g., XHTML, XSL-FO) XSLT Terminology See http://www.dpawson.co.uk/xsl/xslvocab.html Ref. card: http://www.mulberrytech.com/quickref/ “@”: abbreviation for attribute “..”: abbreviation for parent::node() “*”: identifies all of the element children of the context node (@* means all attributes) “.”: abbreviation for self::node() 17 XSL Transformations Assume root element of style sheet is <xsl> Each <xsl> element contains one or more rule elements Each rule has a target and an action Target is a regular expression defining to which XML elements the rule applies Action is the list of flow objects generated when the rule is applied: Actions output a series of HTML tags in combination with the content of the element Actions may output XML tags obtained via transformation of original XML data Actions may output non-markup text, or run simple scripts or programs Actions may use JavaScript to provide more complex, and dynamic behaviors 18 9 XSL Transformations (continued) Conceptual Representation of XSL Transformations: <xsl> <rule> <target-element type=“tagname”/> action </rule> <rule> (…) </rule> </xsl> 19 XSLT and Templates XSLT rules are also called “Templates” There may not be rules to match every element Elements can be reordered on the output. XSL style sheet must be well-formed e.g., a HTML empty tag specified as <br> must be written as <br/> within a XSL style sheet action XSLT elements used as a basis for a simple stylesheet are: <xsl:stylesheet>, <xsl:template match …>, <xsl:apply- templates>, <xsl:for-each select ...>, and <xsl:sort select …> 20 10 XSLT Elements and Functions Creating Elements and Attributes xsl:element, xsl:attribute Iteration and Sorting (e.g., xsl:sort) Conditional Processing xsl:apply-templates select=“ … “, xsl:if, xsl:choose Copying Nodes (e.g., xsl:copy) Combining Stylesheets xsl:import, xsl:include Defining Variables & Parameters (e.g., xsl:variable) Scripting with XPath functions 21 Parsers with XSLT Support SAX 2.0 or DOM Level 2 1.0 Support Required Apache’s Xalan XSLT parser org.apache.xalan.processor/templates/transformer org.apache.xpath Saxon XSLT parser JAXP 1.2 (javax.xml.transform) Transformation API for XML (TraX) Supported by Xalan 2.5.1, and Saxon 6.3 Sun’s XSLTC (donated to Apache and used in Xalan) Converts stylesheet’s to class files (“translets”) 22 11 XSLT APIs 23 XSLT API Packages See Session 3 handout on “Processing XML Documents in Java Using XPath and XSLT” javax.xml.transform Defines the TransformerFactory and Transformer classes, which you use to get a object capable of doing transformations. After creating a transformer object, you invoke its transform() method, providing it with an input (source) and output (result). javax.xml.transform.dom Classes to create input (source) and output (result) objects from a DOM. javax.xml.transform.sax Classes to create input (source) from a SAX parser and output (result) objects from a SAX event handler. javax.xml.transform.stream Classes to create input (source) and output (result) objects from an I/O stream. 24 12 Using TraX http://www-106.ibm.com/developerworks/xml/library/x-tiptrax/ Create source, style, and result objects, and manipulate them using a “Transformer” object Transformer can consume a stream of SAX events to avoid the use of intermediate files or other objects to hold data See complete example using TraX at: http://www.informit.com/isapi/product_id~%7B29A138BF-A72D- 4E3F-AA1C-596EAF7099BD%7D/content/index.asp 25 Part III Extensible Stylesheet Language Formatting Object (XSL-FO) 26 13 XSL Formatting Pipeline XSL-FO is an intermediate form (partial media-dependent output) Java APIs: JAXP + FOP, jFO, XEP, etc. http://www.xmlsoftware.com/xslfo.html 27 XSL Formatting XSL flow objects are markup text Markup language output flow