creating arrays in xslt

Viewed 76862

can array's be created and used in xslt? If so are there suitable examples online to study? If not is there a way to store values in a way that mimics an array?

5 Answers

With XSLT 2.0 you can simply use

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">
    <xsl:output omit-xml-declaration="yes"/>
    <xsl:template match="/">
      <xsl:variable name="array" select="('A','B','C')"/> 
      <xsl:value-of select="$array[2]"/>
    </xsl:template>
</xsl:stylesheet>

If need filter and foreach. (csv example)

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" version="2.0">
   <xsl:output method="text" omit-xml-declaration="yes" />
   <xsl:variable name="array" as="element()*">
      <column name="Company" enable="true">Company</column>
      <column name="User" enable="true">User</column>
   </xsl:variable>
   <xsl:variable name="separator">
      <xsl:text>;</xsl:text>
   </xsl:variable>
   <xsl:variable name="newline">
      <xsl:text>&#xa;</xsl:text>
   </xsl:variable>
   <!-- Output the CSV header -->
   <xsl:for-each select="msxsl:node-set($array)/column[@enable = 'true']">
      <xsl:value-of select="." />
      <xsl:if test="position() != last()">
         <xsl:value-of select="$separator" />
      </xsl:if>
   </xsl:for-each>
   <xsl:value-of select="$newline" />

<!-- your code inserted row -->

</xsl:stylesheet>

Read more

Related