0

i have a hard time doing a collapsed for-each with xsl and xml. Here is my xml file :

<a>
<b>
    <c name="namec1">
        <d name="named1">value1</d>
        <d name="named2">value2</d>
        <d name="named3">value3</d>
        <d name="named4">value4</d>
    </c>
    <c name="namec1">
        <d name="named1">value5</d>
        <d name="named2">value6</d>
        <d name="named3">value7</d>
        <d name="named4">value8</d>
    </c>
    <c name="namec1">
        <d name="named1">value9</d>
        <d name="named2">value10</d>
        <d name="named3">value11</d>
        <d name="named4">value12</d>
    </c>
</b>
</a>

and here is what i want :

namec: [namec1, namec2, namec3]
named: [value1, value5, value9]
named: [value2, value6, value10]
named: [value3, value7, value11]
named: [value4, value8, value12]

Here is my xsl code :

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<xsl:variable name="index" select="." />
<html>
<body>
namec: [
    <xsl:for-each select="a/b">    
        <xsl:choose>
            <xsl:when test="position()=1">
                <xsl:value-of select='c/@name'/>
            </xsl:when>
            <xsl:otherwise>
                , <xsl:value-of select='d/@name'/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:for-each>
    ]   

    <xsl:for-each select="a/b">
        <xsl:if test="position()=1">
            <xsl:for-each select="c/d">
                named: <xsl:value-of select="@name" />,    
                data: [
                <xsl:for-each select="$index/a/b/c">
                    <xsl:value-of select="d" />,
                </xsl:for-each>
                ]
            </xsl:for-each>
        </xsl:if>
    </xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

The first for-each is ok But i don't really understand why the second collapsed for-each is not working I have tried everithing, but i'm new at this... is it even possible ? Thanks in advance !

Tim C
  • 70,053
  • 14
  • 74
  • 93
miloutch
  • 33
  • 1
  • 9
  • **1.** Does each `c` group have the same `d` members? **2.** Are you sure that's the format you want as the output? You have surrounding html tags; that's not going to display the way you show it in HTML. – michael.hor257k Oct 10 '14 at 01:51
  • The d names of each c memebers are the same, but the values are differents. Don't worry about the html tags, they don't matter in my case. the difficulty here is that i want a list looking like that: namedn: [values of all namedn] for n = {1,...,n} – miloutch Oct 10 '14 at 11:09
  • I was just asking if the table was "regular". If it is, then this an issue of *transposing* it - see if you can make this work for you: http://stackoverflow.com/questions/4410084/transpose-swap-x-y-axes-in-html-table – michael.hor257k Oct 10 '14 at 11:29
  • Great answer. i was looking for a way to do that ! i tell you if i manage to apply that in my case. should not be a problem – miloutch Oct 10 '14 at 11:35

1 Answers1

0

I tried to apply this answer to my case and it did work:

The xml:

<table>
  <tr name="date1">
    <td name="var1">A11</td>
    <td name="var2">A12</td>
    <td name="var3">A13</td>
    <td name="var4">A14</td>
    <td name="var5">A15</td>
  </tr>
  <tr name="date2">
    <td name="var1">A21</td>
    <td name="var2">A22</td>
    <td name="var3">A23</td>
    <td name="var4">A24</td>
    <td name="var5">A25</td>
  </tr>
  <tr name="date3">
    <td name="var1">A31</td>
    <td name="var2">A32</td>
    <td name="var3">A33</td>
    <td name="var4">A34</td>
    <td name="var5">A35</td>
  </tr>
</table>

And the xsl:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes"/>
<xsl:template match="/">
categories: [
<xsl:for-each select="table/tr">    
        <xsl:choose>
            <xsl:when test="position()=1">
                '<xsl:value-of select='@name'/>'
            </xsl:when>
            <xsl:otherwise>
                , '<xsl:value-of select='@name'/>'
            </xsl:otherwise>
        </xsl:choose>
    </xsl:for-each>
    ]   



 <xsl:for-each select="table/tr[1]/td">
  <xsl:variable name="vRowPos" select="position()"/>
   <xsl:for-each select="/table/tr">
    <xsl:variable name="vColPos" select="position()"/>
    <xsl:choose>
        <xsl:when test="position()=1">
            name: '<xsl:value-of select="/table/tr[$vColPos]/td[$vRowPos]/@name" />',
            data: ['<xsl:value-of select="/table/tr[$vColPos]/td[$vRowPos]" />'
        </xsl:when>
        <xsl:otherwise>
            , '<xsl:value-of select="/table/tr[$vColPos]/td[$vRowPos]" />'
        </xsl:otherwise>
    </xsl:choose>
   </xsl:for-each>]
 </xsl:for-each>
 </xsl:template>
 </xsl:stylesheet>

and it returns me this:

categories: ['date1', 'date2', 'date3']   

name: 'var1',
data: ['A11', 'A21', 'A31']

name: 'var2',
data: ['A12', 'A22', 'A32']

name: 'var3',
data: ['A13', 'A23', 'A33']

name: 'var4',
data: ['A14', 'A24', 'A34']

name: 'var5',
data: ['A15', 'A25', 'A35']
Community
  • 1
  • 1
miloutch
  • 33
  • 1
  • 9
  • Use "text" as the output method, if that's your final destination. Also, it's good practice to use the `` element to output literal text; that way you do not output the surrounding whitespace. – michael.hor257k Oct 10 '14 at 16:19