XML Editor
Sign up for a WebBoard account Sign Up Keyword Search Search More Options... Options
Chat Rooms Chat Help Help News News Log in to WebBoard Log in Not Logged in
Show tree view Topic
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Postnext
Mehran ZiadlooSubject: Simulating arrays
Author: Mehran Ziadloo
Date: 12 Oct 2005 09:37 AM
Hi,
I'm searching for a way to implement arrays in XSLT.
Consider a situation that you want to select a color from a pre-defined collection, what would you do?
I tried something like this but it didn't work:

<xsl:variable name="Palette">
<Colors>
<Color value="red" />
<Color value="green" />
<Color value="blue" />
</Colors>
</xsl:variable>

And later, I wanted to retrieve one as follow:

<xsl:value-of select="$Palette/Colors/Color[1]" />

It gave me an error, is there any alternative way to do this?

Thanks,
Mehran Ziadloo

Postnext
(Deleted User) Subject: Simulating arrays
Author: (Deleted User)
Date: 12 Oct 2005 10:06 AM
This works fine for me:

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

<xsl:template match="/">
<xsl:variable name="Palette">
<Colors>
<Color value="red" />
<Color value="green" />
<Color value="blue" />
</Colors>
</xsl:variable>
<xml>
<xsl:value-of select="$Palette/Colors/Color[3]/@value" />
</xml>
</xsl:template>

</xsl:stylesheet>

Postnext
Mehran ZiadlooSubject: Simulating arrays
Author: Mehran Ziadloo
Date: 12 Oct 2005 03:29 PM
Well, the fact is I'm testing this with Internet Explorer 6.0 and your code gives out the following error:

Reference to variable or parameter 'Palette' must evaluate to a node list.

Does it mean that there's something wrong with IE or there are some other ways to do this?!

Postnext
(Deleted User) Subject: Simulating arrays
Author: (Deleted User)
Date: 12 Oct 2005 04:54 PM
Try this:

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

<xsl:variable name="Palette">
<Colors>
<Color value="red"/>
<Color value="green"/>
<Color value="blue"/>
</Colors>
</xsl:variable>

<xsl:template match="/">
<xml>
<xsl:value-of xmlns:msxsl="urn:schemas-microsoft-com:xslt" select="msxsl:node-set($Palette)/Colors/Color[3]/@value"/>
</xml>
</xsl:template>
</xsl:stylesheet>

Postnext
(Deleted User) Subject: Simulating arrays
Author: (Deleted User)
Date: 12 Oct 2005 05:37 PM
Ivan just mentioned that there is processor-independent solution:

<?xml version="1.0"?>
<xsl tylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:user="http://mycompany.com/mynamespace"
exclude-result-prefixes="user">
<xsl utput method="xml" indent="yes"/>

<user:Colors>
<user:Color value="red"/>
<user:Color value="green"/>
<user:Color value="blue"/>
</user:Colors>

<xsl:template match="/">
<xsl:value-of select="document('')//user:Colors/user:Color[1]/@value"/>
</xsl:template>
</xsl tylesheet>

Posttop
Mehran ZiadlooSubject: Simulating arrays
Author: Mehran Ziadloo
Date: 13 Oct 2005 01:34 AM
Thank you Yuriy, It was really great help.
But there was a little typing mistakes, so I'm correcting it since it could be usefull for the future:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:user="http://mycompany.com/mynamespace" exclude-result-prefixes="user">
<xsl:output method="xml" indent="yes"/>

<user:Colors>
<user:Color value="red"/>
<user:Color value="green"/>
<user:Color value="blue"/>
</user:Colors>

<xsl:template match="/">
<xsl:value-of select="document('')//user:Colors/user:Color[1]/@value"/>
</xsl:template>
</xsl:stylesheet>

 
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Download A Free Trial of Stylus Studio 6 XML Professional Edition Today! Powered by Stylus Studio, the world's leading XML IDE for XML, XSLT, XQuery, XML Schema, DTD, XPath, WSDL, XHTML, SQL/XML, and XML Mapping!  
go

Log In Options

Site Map | Privacy Policy | Terms of Use | Trademarks
Stylus Scoop XML Newsletter:
W3C Member
Stylus Studio® and DataDirect XQuery ™are from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2016 All Rights Reserved.