Spacko
Spacko

Reputation: 397

XSL Create a table and populate column values from repeated node values

Is it possible to take the XML below and build a table that creates a row for each Attribute node and then places the value of Letter in the correct column?

I tried with an example here, but was not able to get it working.

Thank you.

Note: x shown in table, but could be the actual value

<?xml version="1.0" encoding="utf-8"?>
<Root>
  <AttributeList>
    <Attribute>
      <Name>One</Name>
      <Letter Value="A"/>
    </Attribute>
    <Attribute>
      <Name>Two</Name>
      <Letter Value="B"/>
      <Letter Value="C"/>
    </Attribute>
    <Attribute>
      <Name>Three</Name>
      <Letter Value="A"/>
      <Letter Value="B"/>
      <Letter Value="C"/>
    </Attribute>
    <Attribute>
      <Name>Four</Name>
      <Letter Value="C"/>
    </Attribute>
  </AttributeList>
</Root>
Attribute A B C
One x
Two x x
Three x x x
Four x

My XSLT currently looks like this...

<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
  <xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes"/>
  
  <xsl:variable name="end" select="number(3)"/>
  <xsl:variable name="increment" select="number(1)"/>
  
  <xsl:template match="//Root">
    <html>
      <head/>
      <body>
        <table>
          <tbody>
            <tr>
              <th>Attibute</th>
              <th>A</th>
              <th>B</th>
              <th>C</th>
            </tr>
            
      <xsl:for-each select="//Attribute">
        <tr>
      <td>
          <xsl:value-of select="Name"/>
      </td>
          <xsl:variable name="start" select="number(1)"/>
          <xsl:call-template name="loop">
            <xsl:with-param name="counter" select="$start"/>
          </xsl:call-template>
        </tr>
      </xsl:for-each>
          </tbody>
        </table>
      </body>
    </html>
  </xsl:template>
  
  
  
  <xsl:template name="loop">
    <xsl:param name="counter"/>
    <xsl:if test="$counter &lt;= $end">
      <td>
      <xsl:value-of select="Letter/@Value"/>
      </td>
      <xsl:call-template name="loop">
        <xsl:with-param name="counter" select="$counter + $increment"/>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>

</xsl:transform>

Upvotes: 0

Views: 150

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117073

Could you not do simply:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="utf-8" />
  
<xsl:template match="/Root">
    <html>
        <head/>
        <body>
            <table>
                <thead>
                    <tr>
                        <th>Attribute</th>
                        <th>A</th>
                        <th>B</th>
                        <th>C</th>
                    </tr>
                </thead>
                <tbody>
                    <xsl:for-each select="AttributeList/Attribute">
                        <tr>
                            <td>
                                <xsl:value-of select="Name"/>
                            </td>
                            <td>
                                <xsl:if test="Letter/@Value='A'">x</xsl:if>
                            </td>
                            <td>
                                <xsl:if test="Letter/@Value='B'">x</xsl:if>
                            </td>
                            <td>
                                <xsl:if test="Letter/@Value='C'">x</xsl:if>
                            </td>
                        </tr>
                    </xsl:for-each>
                </tbody>
            </table>
        </body>
    </html>
</xsl:template>

</xsl:stylesheet>

P.S. If you want, you could make this completely generic, with no hard-coding of the values:

XSLT 2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="utf-8" />
  
<xsl:template match="/Root">
    <xsl:variable name="cols" select="distinct-values(AttributeList/Attribute/Letter/@Value)" />
    <html>
        <head/>
        <body>
            <table>
                <thead>
                    <tr>
                        <th>Attribute</th>
                        <xsl:for-each select="$cols">
                            <th>
                                <xsl:value-of select="."/>
                            </th>
                        </xsl:for-each>
                    </tr>
                </thead>
                <tbody>
                    <xsl:for-each select="AttributeList/Attribute">                         
                        <xsl:variable name="letters" select="Letter/@Value" />
                        <tr>
                            <td>
                                <xsl:value-of select="Name"/>
                            </td>
                            <xsl:for-each select="$cols">
                                <td>
                                    <xsl:if test=". = $letters">x</xsl:if>
                                </td>
                            </xsl:for-each>
                        </tr>
                    </xsl:for-each>
                </tbody>
            </table>
        </body>
    </html>
</xsl:template>

</xsl:stylesheet>

Upvotes: 1

Related Questions