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
Conferences Close Tree View
+ Stylus Studio Feature Requests (1192)
+ Stylus Studio Technical Forum (14621)
+ Website Feedback (249)
- XSLT Help and Discussion (7625)
-> + Use of before and after string (3) Sticky Topic
-> - How do I substitute element ty... (1)
-> + How does one add working days ... (4)
-> - Help, I have existing XLT and... (1)
-> + Need help on XSLT issue - (2)
-> + EDI to XML Conversion (7)
-> - XML To JSON Conversion using X... (1)
-> + Formatting Paragraphs to same ... (2)
-> - Grouping of records (1)
-> + Problems with xsd 1.1 (4)
-> + XML to HL7 mapping (3)
-> + XSLT 3 and Iterate (2)
-> + XSL-FO to PDF preview (3)
-> + java.lang.RuntimeException: Er... (2)
-> + Create Acroforms with Stylus X... (2)
-> + How to change XSLT parameter s... (3)
-> + how to change format of the da... (2)
-> + Search "Next 8 Results " doesn... (2)
-> - Support for Git (1)
-> + newbee (8)
-- [1-20] [21-40] [41-60] Next
+ XQuery Help and Discussion (2017)
+ Stylus Studio FAQs (159)
+ Stylus Studio Code Samples & Utilities (364)
+ Stylus Studio Announcements (113)
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>

   
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.