xml-general mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mike Brown <m...@skew.org>
Subject Re: Linking in XSL
Date Tue, 07 Dec 1999 00:25:27 GMT
Mahesh Shantaram wrote:
> How do I declare hypertext references in XSL? [...] What XSL code 
> will link each category and sub-category name to its
> respective HTML document? 

This is an XSLT question, not an issue that spans across all
xml.apache.org projects, so a more appropriate forum is the XSL
list at mulberrytech.com.

Nevertheless, here's your answer. :)

Whoa, slow down, hold on, back up a bit.

It is important that you think of the problem in the following context
before attempting to solve the issue at hand.

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="directory.xsl" ?>
   <category name="Computers" url="index.html">
      <sub-category url="index01.html">Internet</sub-category>
      <sub-category url="index02.html">WWW</sub-category>
      <sub-category url="index03.html">Networking</sub-category>

This XML document would first have to be interpreted as a node tree
using what some might call the XML DOM, although I think the jury is 
still out on what its actual name is or should be.

 |____element 'directory'
       |____text '\n   '
       |____element 'category'
       |     | attribute 'name'='Computers'
       |     | attribute 'url'='index.html'
       |     |____text '\n      '
       |     |____element 'sub-category'
       |     |     | attribute 'url'='index01.html'
       |     |     |____text 'Internet'
       |     |____text '\n      '
       |     |____element 'sub-category'
       |     |     | attribute 'url'='index02.html'
       |     |     |____text 'WWW'
       |     |____text '\n      '
       |     |____element 'sub-category'
       |     |     | attribute 'url'='index03.html'
       |     |     |____text 'Networking'
       |     |____text '\n   '
       |____text '\n'

It is the job of the XML parser to feed this info to the XSL
processor in a way that the XSL processor can deal with, e.g.
using the SAX API, possibly. For convenience, an XSL processor 
may come bundled with an XML parser, but you shouldn't assume 
that an XSL processor is going to always be using an XML 
document for input.

So, now that the XSL processor has available to it this source
tree, it can go about the business of building a new "result" tree
that follows the same model. The XSLT code you write is for telling
the XSL processor how to go about building the result tree. The
XSLT code, too, is interpreted as a node tree, so you shouldn't
assume that it will always be in the form of an XSL document.

In your code you use templates to tell the processor to hop from node to
node, doing something for each one, perhaps, but not necessarily having to
go through them in any particular order or having to do something for any
of them at all, aside from the root node, which is where the processing
will always begin. That's right, you don't necessarily have to make use
of *any* information in the source tree.

After the result tree has been created, a serialized version of
it might be output. XSLT 1.0 recommends, but does not require, that
conforming processors be able to handle two standard output methods: 

So, back to your problem. There really is no such thing as a
"hypertext reference" in XSL. Not yet, anyway. You should redefine
your problem in the context of what an XSL processor actually does
(tree transformation), and then the solution in terms of code
becomes simply a matter of syntax. Let's say you want to output
some very simple HTML with the hyperlinked category name followed
by the hyperlinked subcategories, with <br> elements after each.

"For each element 'category'
  that is a child of element 'directory'
  that is a child of the root node,
I want to add to the result tree:

|____element 'a'
|     | attribute 'href'=(value of attribute 'url')
|     |____text (value of attribute 'name')
|____element 'br'

For each element 'sub-category'
  that is a child of the current 'category' element,
I want to add to the result tree as a siblings that follow
the current 'category' element:

|____element 'a'
|     | attribute 'href'=(value of attribute 'url')
|     |____text (value of text node children)
|____element 'br'

And when the result tree is ready, I want to have it serialized as 
HTML 4.0, if the XSL processor implements the HTML output method."

Code-wise, it could be this:

<?xml version="1.0"?>
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:output method="html" version="4.0" indent="yes"/>
   <xsl:template match="/">
      <xsl:for-each select="/directory/category">
         <a href="{@url}">
            <xsl:value-of select="@name"/>
         <xsl:for-each select="sub-category">
            <a href="{@url}">
               <xsl:value-of select="text()"/>

...or one of many other variations, depending on whether you 
were trying to descend the source tree and process nodes with
different templates or using for-each loops like this and just
plucking things relative to the root node.

Keep in mind that the <a> you see above is shorthand for
<xsl:element name="a"> which is an instruction to add an
element named 'a' to the result tree. It is *not* a literal
specification for output. Same goes for <br/> and href.

Also, and this is unrelated, the processing instruction
<?xml-stylesheet type="text/xsl" href="directory.xsl" ?>
in your XML indicates you might be using IE 5.0, which does 
not implement XSLT 1.0, so the code above will not work as-is. 

And although installing IE 5 results in the media type "text/xsl"
being associated with the filename extension .xsl in your OS,
"text/xsl" is not a standard media type. The current proposal 
is for something like "application/xsl-xml" to be the standard,
but nothing has been finalized yet.

> Any pointers to tutorials?

The XSL Chapter from Elliotte Rusty Harold's XML Bible is a very good free
resource. It is located at 

Crane Softwrights Ltd has a nice tutorial called Practical Transformation
Using XSLT and XPath, which I found extremely helpful. Part of it is free,
but the whole thing will cost you US $40. See http://www.CraneSoftwrights.com

If you learn better by looking at code samples, I highly recommend
Miloslav Nic's site at 

View raw message