Re: xsl script

Subject: Re: xsl script
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Wed, 21 Jun 2000 21:42:10 +0100
Hi,

It is certainly possible to get the output that you want from the input
that you have.

You haven't made it clear exactly what you want the XSLT stylesheet to do,
but I guess that you want to use the template that you've defined within
the 'style' element to display the information that you've defined within
the 'tree' element.  It's not clear how much the information in the 'style'
or in the 'tree' elements can vary, so I'll be making guesses on that.

Before I show you how to do it, I should point out that one of the purposes
of a stylesheet is to define the template that you want the information in
your document to look like.  In the normal state of affairs, I'd expect
your input to be:

<tree>
  <listNews>
    <Title> Titre </Title>
    <Url> URL </Url>
  </listNews>
</tree>

and that information in your stylesheet would turn that into your desired
output.  A stylesheet that would do this would include a template much like
the one that you've defined within your XML file:

<?xml version="1.0" ?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

<xsl:output indent="yes" />

<xsl:template match="tree">
  <data>
    <style>
      <list>
        <b>
          <xsl:value-of select="listNews/Title" />
        </b>
          <xsl:value-of select="listNews/Url" />
        <br />
      </list>
    </style>
  </data>
</xsl:template>

</xsl:stylesheet>

Having said that, you may have a good reason for having your template in
your XML input file, so here is a step-by-step guide to a stylesheet that
uses it.

The document element of an XSLT stylesheet is xsl:stylesheet which looks like:

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
...
</xsl:stylesheet>

The stylesheet is then made up primarily of a number of templates.  Each
template matches against something within your input, and produces some
output as a result.  Templates can be limited so that they match only when
the templates are being applied in a particular 'mode'.

Here is a template that matches the document element (data) in your input:

<xsl:template match="data">
  <data>
    <xsl:apply-templates select="style" />
  </data>
</xsl:template>

This produces a 'data' element, and puts inside it the results of applying
templates from the rest of the stylesheet on the 'style' element.  Next,
then, we define a template that matches the 'style' element:
     
<xsl:template match="style">
  <xsl:copy>
    <xsl:apply-templates mode="copy" />
  </xsl:copy>
</xsl:template>

This makes a copy of itself (using xsl:copy) and puts inside itself the
results of applying templates on its children in the mode 'copy'.  The
children of the 'style' element can be anything, and most of the time, we
just want them to copy themselves and process their own children in the
same way.  Here is the template that does that:

<xsl:template match="*" mode="copy">
  <xsl:copy>
    <xsl:apply-templates mode="copy" />
  </xsl:copy>
</xsl:template>

However, there are two special elements: element-title and element-url.
The 'element-title' element in your input indicates that the Title of the
listNews in the tree should be inserted into the output.  Similarly, the
'element-url' element in your input indicates that the Url of the listNews
in the tree should be inserted.  Here are the templates that do that:

<xsl:template match="element-title" mode="copy">
  <xsl:value-of select="/data/tree/listNews/Title" />
</xsl:template>

<xsl:template match="element-url" mode="copy">
  <xsl:value-of select="/data/tree/listNews/Url" />
</xsl:template>

The full stylesheet is at the bottom of this message.  It produces the
desired output from the input your specify in SAXON.  I'd encourage you,
though, to think about whether you actually want to define your template in
the same file as your data, or whether you should move it into the stylesheet.

I hope this helps,

Jeni

----
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                version="1.0">

<xsl:output indent="yes" />

<xsl:template match="data">
  <data>
    <xsl:apply-templates select="style" />
  </data>
</xsl:template>
                
<xsl:template match="style">
  <xsl:copy>
    <xsl:apply-templates mode="copy" />
  </xsl:copy>
</xsl:template>

<xsl:template match="*" mode="copy">
  <xsl:copy>
    <xsl:apply-templates mode="copy" />
  </xsl:copy>
</xsl:template>

<xsl:template match="element-title" mode="copy">
  <xsl:value-of select="/data/tree/listNews/Title" />
</xsl:template>

<xsl:template match="element-url" mode="copy">
  <xsl:value-of select="/data/tree/listNews/Url" />
</xsl:template>

</xsl:stylesheet>
----
Dr Jeni Tennison
Epistemics Ltd * Strelley Hall * Nottingham * NG8 6PE
tel: 0115 906 1301 * fax: 0115 906 1304 * email: jeni.tennison@xxxxxxxxxxxxxxxx


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread
  • xsl script
    • zze-wokup balr001 - Wed, 21 Jun 2000 20:11:16 +0200
      • <Possible follow-ups>
      • Jeni Tennison - Wed, 21 Jun 2000 21:42:10 +0100 <=