Reputation: 309
I need to loop (xsl:for-each)
through a XML and it will return x nodes that have the same element names and node layout.
I would like to create for each node that I encounter and each value I need a logical parameter that I can address seprately. I tried the array approach, but there is no value returned when I address what should be the corresponding element in the array and the variable does has a value but it looks like one long string without any separators.
So I was wondering if I can create numbered variable names on the fly like variable0, variable1, etc.
I tried to use <xsl:variable name="concat(name, position())" />
but that is not allowed.
Does one know of a way to achieve this or is it absolutely imposible?
thanks
XML data:
<page id="11045138">
<name>SSC NA XML</name>
<description/>
<server>reg6699cic01</server>
<created>2013/11/18 07:16:53 PM</created>
<adhocmessage/>
<workgroups>
<workgroup>
<name>SSC_NA_BWDO_All</name>
<agents>17</agents>
<agentsavailable>2</agentsavailable>
<agentsloggedin>5</agentsloggedin>
<longestavailable>00:01:28</longestavailable>
<longestoutbound>-</longestoutbound>
<longestinbound>-</longestinbound>
<longestnonacd>-</longestnonacd>
<numbernonacd>0</numbernonacd>
<numberoninbound>0</numberoninbound>
<numberoninboundinacw>0</numberoninboundinacw>
<numberonoutbound>0</numberonoutbound>
<numberonoutboundinacw>0</numberonoutboundinacw>
</workgroup>
<workgroup>
<name>SSC_NA_DR_All</name>
<agents>10</agents>
<agentsavailable>0</agentsavailable>
<agentsloggedin>0</agentsloggedin>
<longestavailable>-</longestavailable>
<longestoutbound>-</longestoutbound>
<longestinbound>-</longestinbound>
<longestnonacd>-</longestnonacd>
<numbernonacd>0</numbernonacd>
<numberoninbound>0</numberoninbound>
<numberoninboundinacw>0</numberoninboundinacw>
<numberonoutbound>0</numberonoutbound>
<numberonoutboundinacw>0</numberonoutboundinacw>
</workgroup>
</workgroups>
</page>
XSLT:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="html" encoding="utf-8" indent="yes"/>
<xsl:template match="/">
<xsl:for-each select="//page/workgroups/workgroup">
<xsl:variable name="foo-elements" select="name"/>
<html>
<head>
<title>testing</title>
</head>
<body>
Body Text<br/>
<xsl:value-of select="$foo-elements[position()]"/>
</body>
</html>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
What I require is that the data will be displayed as a table that shows these values (and more) like this
Data elements [0] || Data elements [0+1]
Data elements [0+2] || Data elements [0+3]
/ /
Data elements [0+m] || Data elements [0+n]
It might actualy require 3 rows
Data elements [0] || Data elements [0+1] || Data elements [0+2]
Data elements [0+3] || Data elements [0+4] || Data elements [0+5]
/ /
Data elements [0+k] || Data elements [0+l] || Data elements [0+m]
I hope that explains in brief what I am trying to achieve. I have something that can loop and puts it all underneeth eachother but then the info runs off the wall board that we need to display it on.
Thanks
Upvotes: 0
Views: 848
Reputation: 167706
If you want to create a HTML table with a certain number of columns then you can use that with XSLT 1.0 or 2.0 by processing workgroup[position() mod $row-count = 1]
to create row and . | following-sibling::workgroup[position() < $row-count]
to create a cell (in a different mode):
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="row-count" select="3"/>
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates select="//workgroups"/>
</body>
</html>
</xsl:template>
<xsl:template match="workgroups">
<table>
<thead>
<tr>
<xsl:apply-templates select="descendant::*[position() <= $row-count]" mode="th"/>
</tr>
</thead>
<tbody>
<xsl:apply-templates select="workgroup[position() mod $row-count = 1]"/>
</tbody>
</table>
</xsl:template>
<xsl:template match="*" mode="th">
<th>
<xsl:value-of select="position()"/>
</th>
</xsl:template>
<xsl:template match="workgroup">
<tr>
<xsl:apply-templates select=". | following-sibling::workgroup[position() < $row-count]" mode="cell"/>
</tr>
</xsl:template>
<xsl:template match="workgroup" mode="cell">
<td>
<xsl:value-of select="name"/>
</td>
</xsl:template>
</xsl:stylesheet>
That transforms
<page id="11045138">
<name>SSC NA XML</name>
<description/>
<server>reg6699cic01</server>
<created>2013/11/18 07:16:53 PM</created>
<adhocmessage/>
<workgroups>
<workgroup>
<name>SSC_NA_BWDO_All</name>
<agents>17</agents>
<agentsavailable>2</agentsavailable>
<agentsloggedin>5</agentsloggedin>
<longestavailable>00:01:28</longestavailable>
<longestoutbound>-</longestoutbound>
<longestinbound>-</longestinbound>
<longestnonacd>-</longestnonacd>
<numbernonacd>0</numbernonacd>
<numberoninbound>0</numberoninbound>
<numberoninboundinacw>0</numberoninboundinacw>
<numberonoutbound>0</numberonoutbound>
<numberonoutboundinacw>0</numberonoutboundinacw>
</workgroup>
<workgroup>
<name>SSC_NA_DR_All</name>
<agents>10</agents>
<agentsavailable>0</agentsavailable>
<agentsloggedin>0</agentsloggedin>
<longestavailable>-</longestavailable>
<longestoutbound>-</longestoutbound>
<longestinbound>-</longestinbound>
<longestnonacd>-</longestnonacd>
<numbernonacd>0</numbernonacd>
<numberoninbound>0</numberoninbound>
<numberoninboundinacw>0</numberoninboundinacw>
<numberonoutbound>0</numberonoutbound>
<numberonoutboundinacw>0</numberonoutboundinacw>
</workgroup>
<workgroup>
<name>3</name>
</workgroup>
<workgroup>
<name>4</name>
</workgroup>
<workgroup>
<name>5</name>
</workgroup>
<workgroup>
<name>6</name>
</workgroup>
</workgroups>
</page>
into
<html>
<body>
<table>
<thead>
<tr>
<th>1</th>
<th>2</th>
<th>3</th>
</tr>
</thead>
<tbody>
<tr>
<td>SSC_NA_BWDO_All</td>
<td>SSC_NA_DR_All</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
</tbody>
</table>
</body>
</html>
Upvotes: 1
Reputation: 163458
You can't do it the way you are suggesting. If you tell us what problem you are trying to solve then we can tell you how to solve it.
Upvotes: 0