views:

30

answers:

1

In brief, my problem is that I want to loop through the contents of one child among identical children, but when I try to do so, it loops through the contents of all the children, giving me as many duplicates of the data as there are children.

Example code:

Input.xml

<?xml version="1.0"?>
<base>
    <item name="item1">
        <foo>
            <type1>1</type1>
            <type2>2</type2>
        </foo>
        <bar>
            <type3>3</type3>
        </bar>
    </item>
    <item name="item2">
        <foo>
            <type1>4</type1>
            <type2>5</type2>
        </foo>
        <bar>
            <type3></type3>
            <!-- NOTE! This value is missing. Therefore we must put a blank value in the table-->
        </bar>
    </item>
    <item name="item3">
        <foo>
            <type1>7</type1>
            <type2></type2>
            <!-- NOTE! This value is missing. Therefore we must put a blank value in the table-->
        </foo>
        <bar>
            <type3>9</type3>
        </bar>
    </item>
</base>

tableMaker.xsl

<?xml version="1.0"?>

<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:cyg="http://CygNetSCADA.com/Schemas/CygNetEnterpriseObjects730"&gt;

    <xsl:output method="html" encoding="UTF-8" />

    <xsl:template match="/">
        <html>
            <body>
                <table border="1">
                    <tr>
                        <th>Name</th>
                        <xsl:for-each select="base/item/*/*">
                            <th>
                                <xsl:value-of select="local-name()"/>
                            </th>
                        </xsl:for-each>
                    </tr>

                    <xsl:for-each select="base/item">
                        <tr>
                            <th>
                                <xsl:value-of select="@name"/>
                            </th>

                            <xsl:for-each select="foo/*">
                                <xsl:choose>
                                    <xsl:when test=".[node()]">
                                        <td>
                                            <xsl:value-of select="." />
                                        </td>
                                    </xsl:when>
                                    <xsl:otherwise>
                                        <td />
                                        <!-- This is for that empty value in item3 -->
                                    </xsl:otherwise>
                                </xsl:choose>
                            </xsl:for-each>

                            <xsl:for-each select="bar/*">
                                <xsl:choose>
                                    <xsl:when test=".[node()]">
                                        <td>
                                            <xsl:value-of select="." />
                                        </td>
                                    </xsl:when>
                                    <xsl:otherwise>
                                        <td />
                                        <!-- This is for that empty value in item2 -->
                                    </xsl:otherwise>
                                </xsl:choose>
                            </xsl:for-each>
                        </tr>
                    </xsl:for-each>
                </table>
            </body>
        </html>
    </xsl:template>

</xsl:stylesheet>

What happens in this example is that the generated HTML has 10 columns - one for "name", and "type1", "type2", "type3" three times (three times for the three elements; if there were 4 elements in my input there would be 3 more columns). I only want "type1", "type2", and "type3" to display once in the column. How might I go about doing this?

All help is appreciated and thanks in advance!

+1  A: 

You're using XSL 2.0 so presumably you have access to the xsl:for-each-group element. It's what you need in this situation:

<tr>
  <th>Name</th>
  <xsl:for-each-group select="base/item/*/*" group-by="local-name()">
    <th>
      <xsl:value-of select="current-grouping-key()"/>
    </th>
  </xsl:for-each>
</tr>
Welbog
Excellent! My understanding of some elements of XSLT 2.0 is underwhelming.
adam_0