I am trying to collect strings of my interest by iterating over an array of objects and create a new array. I would use this new array later in my transformation to generate the output.
Below is my code:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:map="http://www.w3.org/2005/xpath-functions/map"
xmlns:array="http://www.w3.org/2005/xpath-functions/array"
version="3.0">
<xsl:output method="text"/>
<xsl:strip-space elements="*" />
<xsl:template name="test" match="/">
<xsl:param name="data" select="data"></xsl:param>
<xsl:variable name="myData" select="$data" />
<xsl:variable name="inputArray" select="array{'John', 'Peter'}"/>
<xsl:variable name="myArray" select="array{}"/>
<xsl:for-each select="1 to array:size($inputArray)">
<xsl:variable name="personName" select="array:get($inputArray, .)" />
<xsl:value-of select="$personName" />
<xsl:variable name="myArray" select="array:append($myArray, personName)" />
</xsl:for-each>
<!-- <xsl:variable name="myArray" select="array:append($myArray, 'John')" />
<xsl:variable name="myArray" select="array:append($myArray, 'Peter')" /> -->
<xsl:for-each select="1 to array:size($myArray)">
<xsl:value-of select="test" />
<xsl:text>Person name is : </xsl:text>
<xsl:value-of select="array:get($myArray,.)" />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
This doesn't append the values into myArray and doesn't even enter the for-each block to output the "test" string.
The source for input array is the array of objects as below:
map {"persons": [map {
"name": "John",
"age": 33,
"residence":"Permanent"
}, map {
"name": "Peter",
"age": 36,
"residence":"Temporary"
}
]
}
I would be reading this array using map:get($persons) iterate and collect the names into myArray.
Given this input variable:
<xsl:variable name="input-map" select="
map {'persons':
[
map {
'name': 'John',
'age': 33,
'residence':'Permanent'
},
map {
'name': 'Peter',
'age': 36,
'residence':'Temporary'
}
]
}"/>
you can create an array of names using:
<xsl:variable name="myArray" select="array{$input-map?persons?*?name}"/>