You are viewing a plain text version of this content. The canonical link for it is here.
Posted to docs@cocoon.apache.org by st...@outerthought.org on 2003/05/15 14:00:03 UTC
[WIKI-UPDATE] FredericGlorieux BeginnerSimpleXSLT Thu May 15 14:00:03 2003
Page: http://wiki.cocoondev.org/Wiki.jsp?page=FredericGlorieux , version: 5 on Thu May 15 11:51:23 2003 by FredericGlorieux
- # [UnderstandingCocoonMounts]
? ^
+ * [UnderstandingCocoonMounts]
? ^
- # [DataDebuggingTechnique]
? ^
+ * [DataDebuggingTechnique]
? ^
+ * [BeginnerSimpleXSLT]
Page: http://wiki.cocoondev.org/Wiki.jsp?page=BeginnerSimpleXSLT , version: 1 on Thu May 15 11:48:31 2003 by FredericGlorieux
New page created:
+ !!! XSLT, how to start ?
+
+ I begin the page, in hope that some others will fill the holes (and correct my english).
+
+ !!!Principles
+ Before, you should know a bit of what XML looks like. So let's take a very short document
+ {{{
+ <article>
+ <title>BeginnerSimpleXSLT</title>
+ <author>FredericGlorieux</author>
+ <abstract>Dummy XML document</abstract>
+ <section>
+ <title>section 1</section>
+ <para>Some <bold>bold</bold> and <italic>italic</italic> text</para>
+ </section>
+ </article>
+ }}}
+
+ This doc is nicer than HTML, especially because I can change the Schema/DTD for my specific usage (<abstract/>, <author/>). Now, how to take advantage of this? XSLT, of course.
+
+ !!Root
+ This will be the root XSL document in which all snippets will now go.
+ {{{
+ <?xml version="1.0" encoding="UTF-8"?>
+ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
+ <xsl:output method="xml" encoding="UTF-8"/>
+ <!-- here put your templates -->
+ </xsl:stylesheet>
+ }}}
+
+
+ {{<?xml version="1.0" encoding="UTF-8"?>}} First declaration that all xml document should have.
+ Note the encoding precision, UTF-8 is default for XML.
+ But languages other than english could have some surprise in a
+ browser. For an XSL, let it as UTF-8, your XML editor should know it.
+
+ {{<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">}}
+ Root element whith a namespace declaration (XML-spec).
+ All elements beginning
+ by xsl:* is mapped as the namespace-uri "http://www.w3.org/1999/XSL/Transform".
+ This is the only string identifying your tags as xslt instructions to process.
+ That means, you can also say
+ {{<myprefix:stylesheet version="1.0" xmlns:myprefix="http://www.w3.org/1999/XSL/Transform"/>}}
+ Could be useful when an xsl should output an xsl.
+ But before that, stay classic, it will be easier for copy/paste (by the way, keep also the @version attribute).
+
+ {{<xsl:output method="xml" encoding="UTF-8"/>}}
+ An xsl:stylesheet is a common XML document, that will be processed.
+ All xsl:* will now be instructions to transform from an input to an output.
+ Input should be valid XML,
+ output could be text, xml, html (with unclosed tags, character entities like ...).
+ This is the reason of @method attribute.
+ Note @encoding, one more time, let UTF-8 for xml usages, iso-8859-1 could be useful if
+ strange browser display.
+
+ !!Pull
+
+ If I put some nice tags in my XML documents, it's probably to use them. Example, I want to extract title|author|abstract of my articles to have a short version. So let's write my first template to __pull__ what I need in the source to output it.
+
+
+ !match, select, XPath
+
+ {{{
+ <!-- this template supposed to be in a xsl:stylesheet described upper --->
+ <xsl:template match="/">
+ <!-- here, we are at root of the xml input, before the first element -->
+ <xsl:copy-of select="."/>
+ </xsl:template>
+ }}}
+
+ {{<xsl:template match="/">}}
+ The xslt engine is a kind of filter, processing an xml input,
+ with the stylesheet instructions. Imagine a complex search/replace, except it's not working
+ on flat text, but on a tree of elements.
+ At first, the engine search for a template matching the root.
+ Thats what is provide here, in the @match, with the "/" value.
+ Unix users will fastly understand the syntax.
+ Now, the current node will be the root of the xml input (like after a "change directory").
+
+ In fact, this very complex template is doing: __nothing__. Output should be quite exactly the input in XML understanding, except encoding or some other properties specified in the xsl:output instruction (only important for text instance of an XML object). Not so unuseful...
+
+ It's also a fast way to debug XSL, to say "Where am I?", "What's in?". The instruction ''copy-of'' output the nodes selected by the expression in @select, here: ".". The dot express current node. It's another important character of expressions, called XPath syntax.
+
+ But don't forget what we want. From the input given upper, imagine we want this short output, with no more content.
+
+ {{{
+ <short>
+ <title>BeginnerSimpleXSLT</title>
+ <author>FredericGlorieux</author>
+ <abstract>Dummy XML document</abstract>
+ </short>
+ }}}
+
+ This template should do the job
+
+ {{{
+ <xsl:template match="/article">
+ <!-- here, we are after the first element -->
+ <short>
+ <xsl:copy-of select="title"/>
+ <xsl:copy-of select="author"/>
+ <xsl:copy-of select="abstract"/>
+ </short>
+ </xsl:template>
+ }}}
+
+ Now, I'm matching XML input with an {{<article/>}} root element (happily, it's my input), this will change the current node inside the template for all new XPath expression (ex: @select). Note alse the <short/> element, which is not in XSLT namespace (no xsl: prefix), so the engine will understand it as an output. And now point the three copy-of, working in the article context, so the @select are respectivly catching and ouputing the {{<title/>, <author/>, <abstract/>}} nodes.
+
+ !!Push
+
+ Pocess for an html output.
+
+ !!!Resources
+
+ Links.
+