Subject: Re: automatic hyperlink with XSLT ? From: Jeni Tennison <Jeni.Tennison@xxxxxxxxxxxxxxxx> Date: Tue, 20 Jun 2000 17:27:21 +0100 |
Jean-Claude, >The following example (from W3C, at http://www.w3.org/TR/xslt#key) >doesn't run on my PC (WinNT4, MIE5). Anybody can help me ? > >XML Doc: ><?xml version="1.0" encoding="UTF-8"?> ><hyperlien> > <prototype name="key" return-type="node-set"> > <arg type="string"/> > <arg type="object"/> > </prototype> > <function>cle</function> ></hyperlien> I notice that you have changed the content of the 'function' element to "cle" rather than "key". That would be fine, but for the example to work properly, you have to be consistent between the content of the 'function' element and the 'name' attribute of the 'prototype' element, which is still "key". More importantly, in order for IE5 to identify the stylesheet to use when you view the XML document and to recognise it as XSLT, you have to add the following processing instruction near the top of your XML (just under the XML Declaration [the first line]): <?xml-stylesheet type="text/xsl" href="stylesheet-name.xsl"?> ^^^^^^^^^^^^^^^^^^^ put the filename of the stylesheet here If you were not using MSXML, this should more accurately be: <?xml-stylesheet type="application/xslt+xml" href="stylesheet-name.xsl"?> but MSXML does not recognise XSLT stylesheets declared in this way. >XSLT : ><?xml version="1.0" encoding="UTF-8"?> ><xsl:stylesheet xmlns:xsl="http://www.w3.org/XSL/Transform/1.0"> > <html> > <head> > <title>Hyperlinks vwith XSLT</title> > </head> > <body> > <xsl:key match="prototype" name="func" use="@name"/> > <xsl:template match="function"> > <b> > <a href="{generate-id(key('func',.))}"> > <xsl:apply-templates/> > </a> > </b> > </xsl:template> > <xsl:template match="prototype"> > <p> > <a name="{generate-id()}"> > <b>function:</b> > </a> > </p> > </xsl:template> > </body> > </html> ></xsl:stylesheet> You have been confused by the lack of context within that particular example, but how you have constructed the stylesheet around the example indicates that you are confused about how stylesheets work generally. Stylesheets work by going through the input document and matching what they find with 'templates' in the stylesheet. Templates usually match against some node (an element, for example) and output some XML (or HTML). The only way that you get output from a stylesheet is by having a template that matches something in your input and actually gives some output. You are trying to output some HTML to wrap around the stuff that is generated by the templates that you lifted from the Recommendation. To output that HTML, you have to place it in a template. The usual way that you would do this is to generate a template that matches against a high-level node in your input, either the 'root node' (the node that represents the document as a whole) or the 'document element' node (the node that represents the highest-level element in your input, 'hyperlien' in your case). You usually use these because that means that applying templates (through a basic xsl:apply-templates) will move the processor on to look at the rest of the content of the document, rather than limit it to a particular little bit. So, you want a template that looks like: <xsl:template match="/"> <html> <head> <title>Hyperlinks with XSLT</title> </head> <body> <xsl:apply-templates /> </body> </html> </xsl:template> This template matches the root node in your input, and outputs the HTML, filling the body with the results of applying templates to all the children of that root node. This includes the document element, 'hyperlien' in your case, which is matched by the default template that basically says 'process the children of this element'. The two other templates, which are then matched, are the ones that you've used from the XSLT Recommendation. xsl:template elements (and xsl:key elements) have to be top-level elements, which means that they are direct children of the xsl:stylesheet element (see stylesheet at end of message). Now, having said all that, you will not be able to use the stylesheet you have created with IE5 because MSXML does not support xsl:key. If you are learning and experimenting with XSL, I recommend that you get your hands on an XSLT processor that supports the majority of XSLT features and sticks closely to the XSLT Recommendation, such as SAXON (from http://users.iclway.co.uk/mhkay/saxon/). I hope this helps, Jeni ---- <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:key match="prototype" name="func" use="@name"/> <xsl:template match="/"> <html> <head> <title>Hyperlinks with XSLT</title> </head> <body> <xsl:apply-templates /> </body> </html> </xsl:template> <xsl:template match="function"> <b> <a href="#{generate-id(key('func',.))}"> <xsl:apply-templates/> </a> </b> </xsl:template> <xsl:template match="prototype"> <p> <a name="{generate-id()}"> <b>function:</b> </a> </p> </xsl:template> </xsl:stylesheet> Dr Jeni Tennison Epistemics Ltd, Strelley Hall, Nottingham, NG8 6PE Telephone 0115 9061301 ? Fax 0115 9061304 ? Email jeni.tennison@xxxxxxxxxxxxxxxx XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
automatic hyperlink with XSLT ?, Jean-Claude Tarby | Thread | RE: automatic hyperlink with XSLT ?, Kay Michael |
RE: automatic hyperlink with XSLT ?, Ben Robb | Date | [no subject], Shravan K. Mishra |
Month |