Sam
Sam

Reputation: 433

How to set column value row by row in xslt?

I am trying to make xslt script which display result in tabular format but I am getting result column value added in next row. please check image below for expected output.

Input xml file:

<?xml version="1.0"?>
<Softwares>  
  <SubNodes>
    <Software>      
      <Results>
        <Info>         
          <Name>Visual Studio</Name>        
          <Key>Name</Key>
          <Value>2010</Value>
        </Info>
        <Info>         
          <Name>Visual Studio</Name>        
          <Key>Driver ID</Key>
          <Value>DI8745</Value>
        </Info>       
      </Results>
    </Software>
    <Software>     
      <Results>
        <Info>         
          <Name>Oracle</Name>        
          <Key>Name</Key>
          <Value>Oracle8</Value>
        </Info>
        <Info>        
          <Name>Oracle</Name>       
          <Key>Driver ID</Key>
          <Value>ID2345</Value>
        </Info>       
      </Results>
    </Software>
    <Software>    
      <Results>
        <Info>         
          <Name>SQL</Name>        
          <Key>Name</Key>
          <Value>SQL2005</Value>
        </Info>
        <Info>         
          <Name>SQL</Name>         
          <Key>Driver ID</Key>
          <Value>ID8888</Value>
        </Info>       
      </Results>
    </Software>   
  </SubNodes>  
</Softwares>

XSLT file:

  <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0"
                    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                    xmlns:w3="http://www.w3.org"
                    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <xsl:output method="html" indent="yes"/>


      <xsl:template match="/">
        <html xmlns="http://www.w3.org/1999/xhtml">
          <head>
          </head>
          <body>        
                <table>
                  <tr>
                    <td>Name</td>
                    <td>Driver ID</td>               
                  </tr>
                  <xsl:for-each select="//SubNodes/Software/Results/Info">
                    <tr>                
                        <td>
                          <xsl:choose>
                            <xsl:when test="Key = 'Name'">
                              <xsl:value-of select="Value" />
                            </xsl:when>
                          </xsl:choose>
                        </td>
                        <td>
                          <xsl:choose>
                            <xsl:when test="Key = 'Driver ID'">
                              <xsl:value-of select="Value" />
                            </xsl:when>
                          </xsl:choose>
                        </td>   
                    </tr>
                  </xsl:for-each>
                </table>         
          </body>
        </html>
      </xsl:template> 
    </xsl:stylesheet>

Current Output: Image shows current output which is not giving result in row by row.

enter image description here

Expected output Image shows expected output which is giving result in row by row. enter image description here

Upvotes: 0

Views: 1199

Answers (1)

Joel M. Lamsen
Joel M. Lamsen

Reputation: 7173

Your XSLT will output a row for every Info. Try this instead:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:w3="http://www.w3.org"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <xsl:output method="html" indent="yes"/>


    <xsl:template match="/">
        <html xmlns="http://www.w3.org/1999/xhtml">
            <head>
            </head>
            <body>        
                <table>
                    <tr>
                        <td>Name</td>
                        <td>Driver ID</td>               
                    </tr>
                    <xsl:for-each select="//SubNodes/Software/Results">
                        <tr>                
                            <td>
                                <xsl:choose>
                                    <xsl:when test="Info[1]/Key = 'Name'">
                                        <xsl:value-of select="Info[1]/Value" />
                                    </xsl:when>
                                </xsl:choose>
                            </td>
                            <td>
                                <xsl:choose>
                                    <xsl:when test="Info[2]/Key = 'Driver ID'">
                                        <xsl:value-of select="Info[2]/Value" />
                                    </xsl:when>
                                </xsl:choose>
                            </td>   
                        </tr>
                    </xsl:for-each>
                </table>         
            </body>
        </html>
    </xsl:template> 
</xsl:stylesheet>

Upvotes: 1

Related Questions