Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 通过列变量的XSLT动态循环_Xml_Xslt - Fatal编程技术网

Xml 通过列变量的XSLT动态循环

Xml 通过列变量的XSLT动态循环,xml,xslt,Xml,Xslt,我希望动态循环使用以下XML来创建以下表结构: 日期|时间|年 2012年4月1日11:25:50 2012年1月1日1:05:20 XML结构将列和行分开,因此我需要传递列描述属性,并使用这些属性来分隔行属性 XML: <Rowsets> <Rowset> <Columns> <Column Description="Date"/> <Column Description="Time"/>

我希望动态循环使用以下XML来创建以下表结构:

日期|时间|年
2012年4月1日11:25:50
2012年1月1日1:05:20

XML结构将列和行分开,因此我需要传递列描述属性,并使用这些属性来分隔行属性

XML:

<Rowsets>
 <Rowset>
    <Columns>
        <Column Description="Date"/>
        <Column Description="Time"/>
        <Column Description="Year"/>
    </Columns>
    <Row>
        <Date>01/04</Date>
        <Time>11:25:50</Time>
        <Year>2012</Year>
    </Row>
    <Row>
        <Date>01/01</Date>
        <Time>1:05:20</Time>
        <Year>2012</Year>
    </Row>
 </Rowset>
</Rowsets>

01/04
11:25:50
2012
01/01
1:05:20
2012
My AttemptedXSLT,它创建标题,但将行数据集中到一个列中:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
 <html>  
  <body>
    <table border="1" cellspacing="0">
      <!-- Generate the row of table header cells -->
      <tr>
    <xsl:variable name="currentvar" select="//@Description[1]"/>
        <xsl:for-each select="$currentvar">
          <th>
            <xsl:value-of select="."/>
          </th>
        </xsl:for-each>
      </tr>
  <xsl:for-each select="Rowsets/Rowset/Row">
   <tr>
    <td>
              <xsl:value-of select="."/>
    </td>
       </tr>
      </xsl:for-each>       
  </table> 
  </body>
 </html>
</xsl:template>
</xsl:stylesheet>


感谢您的帮助,谢谢

这是因为您试图选择
元素的全部内容。替换

<td><xsl:value-of select="."/></td>



将解决您的问题。

这是因为您试图选择
元素的全部内容。替换

<td><xsl:value-of select="."/></td>



将解决您的问题。

通过将表格的每个部分分解为自己的模板,可以大大简化样式表。此样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:strip-space elements="*"/>
    <xsl:template match="/">
        <html>
            <body>
                <table border="1" cellspacing="0">
                    <xsl:apply-templates/>
                </table>
            </body>
        </html>
    </xsl:template>
    <xsl:template match="Columns|Row">
        <tr><xsl:apply-templates/></tr>
    </xsl:template>
    <xsl:template match="Columns/*">
        <th><xsl:apply-templates select="@Description"/></th>
    </xsl:template>
    <xsl:template match="Row/*">
        <td><xsl:apply-templates/></td>
    </xsl:template>
</xsl:stylesheet>

产生所需的结果:

<html>
   <body>
      <table border="1" cellspacing="0">
         <tr>
            <th>Date</th>
            <th>Time</th>
            <th>Year</th>
         </tr>
         <tr>
            <td>01/04</td>
            <td>11:25:50</td>
            <td>2012</td>
         </tr>
         <tr>
            <td>01/01</td>
            <td>1:05:20</td>
            <td>2012</td>
         </tr>
      </table>
   </body>
</html>

日期
时间
年
01/04
11:25:50
2012
01/01
1:05:20
2012

解释:注意,我们让XSLT处理器完成大部分工作。我们只需在文档的每个级别应用模板,这将处理过程移动到上下文节点的子节点。(由于XSLT的原因,不需要显式循环任何元素或输出任何文本。)然后,我们在自己的模板中处理每个表节。这是一种更加灵活、易于维护和可读的方法。

通过将表的每个部分分解为自己的模板,可以大大简化样式表。此样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:strip-space elements="*"/>
    <xsl:template match="/">
        <html>
            <body>
                <table border="1" cellspacing="0">
                    <xsl:apply-templates/>
                </table>
            </body>
        </html>
    </xsl:template>
    <xsl:template match="Columns|Row">
        <tr><xsl:apply-templates/></tr>
    </xsl:template>
    <xsl:template match="Columns/*">
        <th><xsl:apply-templates select="@Description"/></th>
    </xsl:template>
    <xsl:template match="Row/*">
        <td><xsl:apply-templates/></td>
    </xsl:template>
</xsl:stylesheet>

产生所需的结果:

<html>
   <body>
      <table border="1" cellspacing="0">
         <tr>
            <th>Date</th>
            <th>Time</th>
            <th>Year</th>
         </tr>
         <tr>
            <td>01/04</td>
            <td>11:25:50</td>
            <td>2012</td>
         </tr>
         <tr>
            <td>01/01</td>
            <td>1:05:20</td>
            <td>2012</td>
         </tr>
      </table>
   </body>
</html>

日期
时间
年
01/04
11:25:50
2012
01/01
1:05:20
2012

解释:注意,我们让XSLT处理器完成大部分工作。我们只需在文档的每个级别应用模板,这将处理过程移动到上下文节点的子节点。(由于XSLT的原因,不需要显式循环任何元素或输出任何文本。)然后,我们在自己的模板中处理每个表节。这是一种更加灵活、易于维护和可读的方法。

您好,我如何用and分隔标题和正文?例如:``DATETIMEYEAR````01/0411:25:502012``01/011:05:202012````@user1130511-我回答了您的新问题:您好,我如何用and分隔标题和正文?例如:``DATETIMEYEAR````01/0411:25:502012``01/011:05:202012``````@user1130511-我回答了你的新问题: