RE: [xsl] fill an array in javascript

Subject: RE: [xsl] fill an array in javascript
From: "Chris Bayes" <chris@xxxxxxxxxxx>
Date: Fri, 23 Nov 2001 12:47:25 -0000
Nathalie,
If you are doing what it looks like you are trying to do you just need
<xsl:template name="TitreTexte">
	XT_HTML = new Array(<xsl:value-of select="titre"/>, 
		              new Array(
		<xsl:for-each select="texte">
                                    "<xsl:value-of select="." />"
		</xsl:for-each>
                                   )
                         )
</xsl:template>
Which will give you an array that looks like
[Title][[FirstText][SecondText][ThirdText]]
Accessed as
var title = XT_HTML[0];
var first = XT_HTML[1][0];
var second = XT_HTML[1][1];
var third = XT_HTML[1][2];

Ciao Chris

XML/XSL Portal
http://www.bayes.co.uk/xml


> -----Original Message-----
> From: owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx 
> [mailto:owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx] On Behalf Of 
> Nathalie Pult
> Sent: 23 November 2001 10:43
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] fill an array in javascript
> 
> 
> Hi 
> I have to create an array and fill it with some text chosen 
> out of an xml-file. A javascript-function() calls a named 
> template that should get the wanted text()-Elements out of 
> the .xml. But I have problems with variables and their 
> concat-function, and I'm not sure if the whole template will work.
> 
> Thanks for helping!
> 
> Nathalie
>  
> I have the following code for the .xsl:
> <?xsl version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet
> version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
> 	<xsl:output
> encoding="UTF-8" method="xml" version="4.0" indent="no" 
> media-type="text/html"/>
> 	<xsl:template match="/">
> 		<html>
> 			<head>
> 				<title>xml-generated 
> module</title><script type="text/javascript" 
> language="JavaScript"> funcition init() { <xsl:call-template 
> name="TitreTexte"/> } [...] some more javascript
> 				</script>
> 			</head>
> 			[Frameset
> 		</html>
> 	</xsl:template>
> 
> 	<xsl:template name="TitreTexte">
> 		<xsl:text>var XT_HTML = new Array(10);</xsl:text>
> 		<!--how can I create an Array whitout knowing the
> exact amount
> of items?-->
> 
> 		<xsl:variable name="firstArray">
> 			<xsl:for-each
> select="//texte_initial/titre[position() &gt; 1]">
> 			<xsl:text>XT_HTML[position()] = new
> Array(2);</xsl:text>
> 				<xsl:variable
> name="selectedTitle" select="text()">
> 					<xsl:variable
> name="firstArray" select="concat('XT_HTML[' , position(), 
> '][1]=&apos;', $selectedTitle, '&apos;')"></xsl:variable>
> 					<xsl:value-of
> select="$firstArray"/>
> 				</xsl:variable>
> 				<xsl:variable name="selectedTexts">
> 					<xsl:value-of
> select="../texte[1]/text()"/>
> 					<xsl:for-each
> select="../texte[position() &gt; 1]">
> 						<xsl:value-of
> select="text()"/>
> 					</xsl:for-each>
> 					<xsl:variable
> name="secondArray" select="concat('XT_HTML[', 
> position(),'][2]=&apos;', $selectedTexts, '&apos;')"></xsl:variable>
> 					<xsl:value-of
> select="$secondArray"/>
> 				</xsl:variable>
> 			</xsl:for-each>
> 		</xsl:variable>
> 	</xsl:template>
> </xsl:stylesheet>
> 
> And the .xml that goes with:
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <modul>
> 	<phonetique>
> 		<lecon id="10">
> 			<texte_initial id="11">
> 				<titre>Title</titre>
> 				<texte>FirstText</texte>
> 				<texte>SecondText</texte>
> 				<texte>ThirdText</texte>
> 			</texte_initial>
> 		</lecon>
> 	</phonetique>
> </modul>
> 
> 
> 
> 
> 
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
> 
> 


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


Current Thread