[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: An approach to XSLT programming, was Re: Dy
Dave,
At 03:22 AM 11/29/2010, you wrote: On Sun, 28 Nov 2010 17:52:03 +0000 Michael Kay <mike@xxxxxxxxxxxx> wrote: Yes: in terms of the tree. In this case "I want to process a node based on conditions defined on another node associated with it by means of labels carried on attributes". This is spatial rather than temporal; in training ourselves to think this way, it helps to keep in mind that the tree traversal can happen in any order (temporally), so the problem is the same whether the node associations look "backward" or "forward". The solution (as suggested) is to use a key to retrieve the associated node -- in this case, probably to apply templates to it. So (if this problem demands HTML/CSS): <styles> <style name="basic" typeface="Optima" weight="normal" slant="roman"/> <style name="emphatic" typeface="Optima" weight="bold" slant="italic"/> </styles> . . . <p style="basic">This paragraph contains some <span style="emphatic">emphatic</span> text.</p> <xsl:key name="style-by-name" match="style" use="@name"/> <xsl:template match="p | span"> <xsl:copy> <xsl:apply-templates select="key('style-by-name',@style)" mode="css-style"/> <xsl:apply-templates/> </xsl:copy> </xsl:template> <xsl:template match="style" mode="css-style"> <xsl:attribute name="style"> <xsl:apply-templates select="@*" mode="css-style"/> <xsl:attribute> </xsl:template> <xsl:template match="@typeface" mode="css-style"> <xsl:text>font-family: </xsl:text> <xsl:value-of select="."/> <xsl:text>;</xsl:text> </xsl:template> etc. It's conceptually neat, and easy to maintain. While, if you think temporally, it seems inefficient (the same nodes get visited repeatedly), it also relies on XSLT constructs that admit of a high degree of optimization in the processor. Cheers, Wendell ====================================================================== Wendell Piez mailto:wapiez@xxxxxxxxxxxxxxxx Mulberry Technologies, Inc. http://www.mulberrytech.com 17 West Jefferson Street Direct Phone: 301/315-9635 Suite 207 Phone: 301/315-9631 Rockville, MD 20850 Fax: 301/315-8285 ---------------------------------------------------------------------- Mulberry Technologies: A Consultancy Specializing in SGML and XML ======================================================================
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|