Re: [xsl] copy of specific elements

Subject: Re: [xsl] copy of specific elements
From: Norman Gray <norman@xxxxxxxxxxxxxxx>
Date: Mon, 18 Aug 2008 16:43:43 +0100

On 2008 Aug 18, at 14:21, henry human wrote:

I tried with the FAQ and i understood it so as i am
witing the code snippet bellow!
Here is the sample.
In this loop it should turns 2 times because there are
2 sections and than displays the
author names in:
author[1] and
(this is the array: [@type='author'][position()])

This account doesn't really match what the sample XSLT is trying to do, but I think I understand what you're after. Consider the following:

% cat hh.xml
    <element type="author">Author number 1</element>
    <element type="author">Author number 2</element>
    <element type="section">section 1</element>
    <element type="section">section 2</element>
% cat hh.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
  <xsl:output method="html"/>
  <xsl:template match="/">
  <xsl:template match='element'>
    <p>Element: <xsl:apply-templates/></p>
% xsltproc hh.xslt hh.xml
<p>Element: section 1</p>
<p>Element: section 2</p>

(Did you want something to happen to the <author> elements? It's not at all clear)

I think that your fundamental problem is that you're thinking of 'arrays' and 'indexes' in a language where this isn't natural. You probably don't need to use position() at all.

In XSL, using for-each is almost always the wrong (or at least an unnatural) way of doing things. Of course, 'wrong' here is a bit of an exaggeration, but it does seem to be true that the people who use xsl:for-each the most are those new to the language.

XSL is a templating language, specialised for processing trees of elements, and that means that implicit loop within the xsl:apply- templates is usually the most suited to the job.

Best wishes,


-- Norman Gray : Physics and Astronomy, University of Leicester

Current Thread