简体   繁体   中英

Using an XSL for-each on identical XML children

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:

<?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>

<?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">

    <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!

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM