如何在xslt中逐行设置列值?

时间:2014-01-31 06:21:57

标签: xslt-1.0

我正在尝试制作以表格格式显示结果的xslt脚本,但我在下一行中添加了结果列值。请检查下面的图像以获得预期的输出。

输入xml文件:

<?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文件:

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

电流输出: 该图显示了当前输出没有逐行给出结果。

enter image description here

预期产出 图像显示了逐行给出结果的预期输出。 enter image description here

1 个答案:

答案 0 :(得分:1)

您的XSLT将为每Info输出一行。试试这个:

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