Subject: Re: [xsl] explain of Joerg xslt (was: xsl:copy ...) From: "Carsten Klein" <carstenklein@xxxxxxxx> Date: Mon, 8 Apr 2002 01:45:34 +0200 |
Hi Matias, well, a lot of questions =) Thanks for the roses for the snippet of xsl code =) Okay, here we go, you got the xsl source, therefore I am not repeating it here. : Lines 8 to 13. We are inside each "Indice" branch. : i) which is the difference between match and select attr of xsl:template? The match attribute to xsl:template instructs the xsl processor to apply this template to all nodes in the source document which match the "match" clause. A valid (XPath) expression evaluating to a node-set. Select does not apply to xsl:template. It does apply to xsl:call-template or xsl:apply-templates and xsl:for-each, have a look at http://www.w3.org/TR/2001/WD-xslt11-20010824/. This helped me a lot, to get over the first difficulties of programming xsl, and introducing new difficulties in programming xsl =) Okay, the select attribute does the same as a match attribute, in a certain way. But only to the extend of selecting / matching a given nodeset in the source document. Select does, in contrast to match, provide you with the ability to parse other nodes in the source document, while you are still in a template which naturally processes another node, which matched the "match" expression for this template. This is done so in line 8-13. The template is processed for each Indice node in your source document. It then creates the start tag of a <tree> node in the output tree and will apply all templates which match attribute nodes for the currently selected <Indice/> element. Whilst preforming the transformation on the Indice element, this template now "calls" other templates which may match any attribute node <xsl:apply-templates select="@*"/>. The current template is still in the <Indice> element's context, therefore the processing does not stop here for the currently selected <Indice/> element. Instead it is merely postponed until the templates which match any occurence of @* are processed. The following <xsl:apply-templates select="key('Indice', @IdIndice)"/> will do the same as <xsl:apply-template select="//TreeData/Indice[@IdRef = @IdIndice]"/> and select all nodes in the source document which match the above expression. Using keys is a bit different. Think of keys as a means to provide easy access like using a filer indexed by name or else. You create a key, which eventually represents a node set consisting of nodes in the input document, wich Joerg defined like this: <xsl:key name="Indice" match="Indice" use="@IdRef"/> The key with the name "Indice" matches all Indice nodes inside your document which have a @IdRef attribute node. The processor (as far as I understood) will then have faster access to the node-set defined by this key, keeping a copy of the nodes matching in memory, since it will evaluate the expression @IdRef only once. The node-set will then be at the ready when referring to a node or node-set inside this key. : ii) Why the Indice tag is changing for the tree tag? Only by putting the : <tree> tag inside the <xsl:template> which matchs the tag name Indice will : do the trick? This syntax wants to say: Match all Indice tags and when you : find each one parse and write <tree> <xsl:appl... </tree> instead of what : the Indice tag has inside it? hope you understand this question :) Well, basically when you write xsl to transform an xml document to another document type, be it xml or html or else, you process an input document by writing rules (templates) of how this document is to be transformed. Therefore it is up to you to decide what will be inside the output document and what not. <xsl:....> type of elements are mere instructions to the xsl processor to process the document you feed into it with the given stylesheet. You are free to use whatever you want to use in your stylesheet to have a output document (tree) matching your needs. Therefore it is absolutely allowed to insert a <tree> tag into the stylesheet, as long as you close this tag =) Whatever you write into the stylesheet to be output in the output document (tree), it will be in there. : iii) line 10. I cannot understand the concept of apply-templates. : this wants to say: Apply templates to all attributes inside Indice.? is this : done in order to make possible the apply of line 14-16? Well <xsl:apply-templates ../> does instruct the xsl processor to do further processing. If you had written the stylesheet (the first template which matches "/") as follows: <xsl:template match="/"> <tree> </tree> </xsl:template> What would the processor do with your input document. And would your result be the same? Nope, sir, it will not, it will just ouput a <?xml encoding="..." version="..."> <tree/> and nothing more, since the processor by itself will only parse the document using the template which matches the root node, which to my opinion would be <TreeData/>, but it is a different node, created at the time the processor is invoked and does not at all relate to <TreeData/>. Had a hard time understanding this, but the specifications leave no doubt: "When the source tree is created by parsing a well-formed XML document, the root node of the source tree will automatically satisfy the normal restrictions of having no text node children and exactly one element child. When the source tree is created in some other way, for example by using the DOM, the usual restrictions are relaxed for the source tree as for the result tree." This means the root node is something else, something different and definitely not <TreeData/> as this element will be the root nodes only child element. This is what identifies a well formed and valid xml document. A root node having only one child node. At that time the processor may or may not know about any Indice nodes inside your document tree. You have to tell the processor by using <xsl:apply-templates/> to apply templates to all nodes beneath the root node of your document. Only then the processor is able to transform the whole document tree. This, of course, depends on your select and match statements. You could, for example write a stylesheet which select only a specific set of Indice nodes (e.g. only the recently deceased patients =) and output a list of those patient for whom there was no cure whatsoever. : iv) line 11. this means: apply templates to all Indice tags inside the : Indice tag being parsed. ?this is recursive, isn't it? =) yes. 15. differences between xsl:copy and xsl:copy-of. Uhoh, don't know, have a quick look at the reference manual which states: xsl:copy -------- "[...]The xsl:copy element provides an easy way of copying the current node. Instantiating the xsl:copy element creates a copy of the current node. The namespace nodes of the current node are automatically copied as well, but the attributes and children of the node are not automatically copied. The content of the xsl:copy element is a template for the attributes and children of the created node; the content is instantiated only for nodes of types that can have attributes or children (i.e. root nodes and element nodes).[...]" xsl:copy-of ----------- "[...]The xsl:copy-of element can be used to insert a node-set into the result tree, without first converting it to a string as xsl:value-of does (see 7.6.1 Generating Text with xsl:value-of). The required select attribute contains an expression. When the result of evaluating the expression is a node-set, all the nodes in the set are copied in document order into the result tree; copying an element node copies the attribute nodes, namespace nodes and children of the element node as well as the element node itself; a root node is copied by copying its children. For copying an attribute or namespace node, the same rules apply as with xsl:copy (see 7.5 Copying). When the result is not a node-set, the result is converted to a string and then inserted into the result tree, as with xsl:value-of.[...]" As you can see, xsl:copy creates a copy of the current node. xsl:copy-of does have a select attribute which enables you to copy a set of nodes directly to the output tree without touching them, except for what the processor will apply to the nodes in the output tree (see above). : vi) line 15. The "dot" means: the content inside the attributes? The use of dot is equal to the use of current() function. It selects the current node. Have a look at http://www.w3.org/TR/1999/REC-xpath-19991116 (XML Path Language XPath) This document gives a few examples on XPath expressions and an indepth discussion on the specifications of the language. Hell this is Backup-Naur form, I had a hard time understanding this...=) Okay hope my "kauderwelsch" is easy to understand and does not state that much of incorrect information as I wish it could =) Bye Carsten _________________________________________________________ Do You Yahoo!? Get your free @yahoo.com address at http://mail.yahoo.com XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
RE: [xsl] explain of Joerg xslt (wa, Matias Woloski | Thread | RE: [xsl] explain of Joerg xslt (wa, Matias Woloski |
Re: [xsl] Removing namespaces from , Christopher R. Maden | Date | RE: [xsl] explain of Joerg xslt (wa, Matias Woloski |
Month |