[Home] [By Thread] [By Date] [Recent Entries]
Greetings.
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 author[2] (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
<document>
<elements>
<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>
</elements>
</document>
% cat hh.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates
select='document/elements/element[@type="section"]'/>
</body>
</html>
</xsl:template>
<xsl:template match='element'>
<p>Element: <xsl:apply-templates/></p>
</xsl:template>
</xsl:stylesheet>
% xsltproc hh.xslt hh.xml
<html><body>
<p>Element: section 1</p>
<p>Element: section 2</p>
</body></html>
%(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
|

Cart



