XSLT - arrays and iterations

How can I create an array and iterate through it and get its values?


<xsl:variable name="element"> <xsl:for-each select="document/items/item"> <item> <xsl:choose> <xsl:when test="/@attr">1</xsl:when> <xsl:otherwise>0</xsl:otherwise> </xsl:choose> </item> </xsl:for-each> </xsl:variable> <xsl:for-each select="$element"> <p><xsl:value-of select="."/></p> </xsl:for-each>

Actual Output:


Should be:

<p>0</p> <p>1</p> <p>0</p> <p>0</p>

EDIT: the array should be dynamically generated!



<xsl:variable name="element">
<xsl:for-each select="document/items/item">
<xsl:when test="/@attr">1</xsl:when>

and an XSLT 1.0 processor your variable is of type result tree fragment where all you can do is use <xsl:copy-of select="$element"/> or <xsl:value-of select="$element"/> or where you need to use an extension function like exsl:node-set to convert the result tree fragment into a node-set so

<xsl:for-each select="exsl:node-set($element)/item">
<p><xsl:value-of select="."/></p>

should do (with the xmlns:exsl="http://exslt.org/common" declared in your stylesheet).

With XSLT 2.0 or 3.0 your variable is a temporary tree with a root node containing some item element so there you don't need an extension function but you need to address the item elements e.g.

<xsl:for-each select="$element/item">
<p><xsl:value-of select="."/></p>

As an alternative in XSLT 2.0/3.0 you can change the variable declaration to

<xsl:variable name="element" as="element(item)*">
<xsl:for-each select="document/items/item">
<xsl:when test="/@attr">1</xsl:when>

to have a variable with a sequence of item elements, in that case your

<xsl:for-each select="$element">
<p><xsl:value-of select="."/></p>

would do what you want.

There are no arrays in XSLT 1.0 or 2.0.


Please share the XML which needs to be transformed . IT will help .


<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<xsl:for-each select="document/items/item">
<xsl:when test="/@attr">1</xsl:when>

Category:xslt Time:2019-01-09 Views:3
Tags: xslt

Related post

  • Squeak String array and iteration 2012-04-24

    I've created a subclass of Array and written some messages for it that have worked out well so far. However, I'm now having trouble with my last message Here's the body of my code: startString: charToTest "Returns a list of all the words that start w

  • In Java, how to find a string from an string array and why is the below example not working as expected 2012-02-15

    how to find a string from an string array and In the below example i am always get search failure except search for first string but not others like second or third , i even tried with [indexof !=1 condition instead of equals ] but it remains same re

  • Fast container for setting bits in a sparse domain, and iterating (C++)? 2008-11-22

    I need a fast container with only two operations. Inserting keys on from a very sparse domain (all 32bit integers, and approx. 100 are set at a given time), and iterating over the inserted keys. It should deal with a lot of insertions which hit the s

  • What are the differences between a multidimensional array and an array of arrays in C#? 2009-02-28

    What are the differences between multidimensional arrays double[,] and array-of-arrays double[][] in C#? If there is a difference, what is the best use for each one? --------------Solutions------------- Array of arrays (jagged arrays) are faster than

  • Code Separation Paradox: Create HTML Tree from Multi-Dimensional Array AND Keep the HTML Outside of the Recursive Function 2009-05-21

    This working code seems to be the typical solution to this problem. It takes a multi-dimensional array that holds categories and their subcategories (with no implied limitation on how many levels deep it goes) and creates an HTML unordered list from

  • I have two unordered integer arrays, and i need to know how many integers these arrays have in common 2009-07-03

    I'm working in a LAMP environment, so PHP is the language; at least i can use python. As the title said i have two unordered integer arrays. $array_A = array(13, 4, 59, 38, 9, 69, 72, 93, 1, 3, 5) $array_B = array(29, 72, 21, 3, 6) I want to know how

  • How to go through an array and add their values 2009-08-04

    I was wondering how is it possible to go through the values of an array and add their values together. var arr = [1, 2, 3, 4]; should I use var add = $.each(arr, function() { }); but how can I add the values together. thanks --------------Solutions--

  • trouble seperating loader array and array for grid of images 2009-12-15

    My problem is that I can't figure out how to decouple the loader from this loadNewRow function. Ideally, I'd like to figure out how to load all the swf's give them dynamic names... like thmb1loaded, thmb2loaded, thmb3loaded.. push into an a ("loaded"

  • Iterate over array and combine two arrays 2010-01-07

    I have two arrays of objects. I would like iterate over each of these two arrays and add a property ("IsPriced"). Then I would like to combine the two arrays into one array. How can I do this in JavaScript (with MooTools is fine, but not with jQuery

  • Difference between Array and ArrayCollection(Flex) 2010-02-22

    What's the difference between array and arraycollection? --------------Solutions------------- First, the ArrayCollection is designed for the Flex framework. It acts like a Proxy for a given Array instance. This means it can listen for modifications m

  • Common Ancestor to Java Array and List 2010-03-14

    In .NET, both array and list have Enumerable as ancestor, so a method that accept Enumerable as an argument can receive both array and list as its argument. I wonder if there is a similar thing in Java? --------------Solutions------------- No, there'

  • PHP array and foreach help 2010-03-22

    I need some help with an array and a foreach loop, I have an array that is data returned from a database, basically it is a list of titles and images associated with it, it looks a little like this, Array ( [0] => Array ( [contentImageId] => 28

  • Difference between Array and List in scala 2010-04-26

    In what cases I should use Array(Buffer) and List(Buffer). Only one difference that I know is that arrays are nonvariant and lists are covariant. But what about performance and some other characteristics? --------------Solutions------------- Immutabl

  • BASH, multiple arrays and a loop 2010-04-30

    At work, we have 7 or 8 hardrives we dispatch over the country, each have unique labels which are not sequential. Ideally drives are plugged in our desktop, then gets folders from the server that correspond to the drive name. Sometimes, only one hard

  • array and point problem 2010-05-30

    Here, I have a bad program. Its outputs confusing me, anyone can tell me why ? #include <stdio.h> #include <stdlib.h> int main() { int i = 0; char *a_result[10]; char *b_result[10]; for (i = 0; i < 10; i++) { char a_array[10]; char *b_

  • Difference between Array and Array Collection in Flex 2010-07-16

    While making my choice b/n Array and Arraycollection I get confused why whould I use one and why not another. I have read the theory in langref but apart from that is there some general advantages/disadvantages of one over the another that you have l

  • Converting tree-like object structure into an array and interating the whole thing 2010-07-21

    I have a tree-like object structure that consists of two types of objects: object of class Category object of class CategoryLink The structure is the following: The whole story begins with an array of Categories that have no parent Each Category has

  • What's the technical difference between jQuery arrays and jQuery objects? 2010-07-21

    Of these two, which is correct? jQuery('someclass').click(function() { alert("I've been clicked!"); }); or jQuery('someclass').each().click(function() { alert("I've been clicked!"); }); I'm wondering if I have a fundamental misunderstanding of how ce

  • difference between array and list 2010-08-15

    what is the difference between array and list? --------------Solutions------------- In C, an array is a fixed-size region of contiguous storage containing multiple objects, one after the other. This array is an "object" in the meaning which C gives t

  • String array and Arraylist/list in C++ 2010-09-03

    I am new to C++, can anybody provide me some sample program's for String array and Arraylist/list in C++ Thanks in Advance. --------------Solutions------------- You can refer the following urls, 1. String 2. Character Sequence 3. STL You can use std:

Copyright (C) pcaskme.com, All Rights Reserved.

processed in 0.183 (s). 13 q(s)