Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.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
C# XSLT使用动态行和列将XML转换为HTML_C#_Html_Xml_Xslt - Fatal编程技术网

C# XSLT使用动态行和列将XML转换为HTML

C# XSLT使用动态行和列将XML转换为HTML,c#,html,xml,xslt,C#,Html,Xml,Xslt,我的XML格式如下: <Items> <Item name="Item1"> <Label>Label 1</Label> </Item> <Item name="Item2"> <Label>Label 2</Label> </Item> <ColumnBreak></ColumnBreak>

我的XML格式如下:

<Items>
    <Item name="Item1">
        <Label>Label 1</Label>
    </Item> 
    <Item name="Item2">
        <Label>Label 2</Label>
    </Item>
    <ColumnBreak></ColumnBreak>
    <Item name="Item3">
        <Label>Label 3</Label>
    </Item>
    <Item name="Item4">
        <Label>Label 4</Label>
    </Item>
</Items>

例如,如果在XML中的Item3之后设置了
,则生成的div应该有3行2列,第2列只显示标签4。

用简单的代码回答我自己的问题。使用HTML表而不是div

 <xsl:template match="/Items">
    <xsl:variable name="cols" select="round(12 div (count(ColumnBreak) + 1))"/>
    <xsl:variable name="rows" select="round(count(Item) div (count(ColumnBreak) + 1))" />
    <table>
      <xsl:for-each select="Item[position() &lt;= $rows]">
        <xsl:variable name="x" select="position() mod $rows"/>
        <tr>
            <xsl:for-each select="../Item[position() mod $rows = $x]">
              <td style="col-md-{$cols}">
                  <xsl:value-of select="."/>
              </td>
            </xsl:for-each>
        </tr>
      </xsl:for-each>
    </table>
 </xsl:template>


您使用的是哪个版本的XSLT?
ColumnBreak
是否出现在相同数量的
元素之后?@AniketV是的,它出现在相同数量的元素之后您是否使用XSLT 2.0?@AniketV版本1
 <xsl:template match="/Items">
    <xsl:variable name="cols" select="round(12 div (count(ColumnBreak) + 1))"/>
    <xsl:variable name="rows" select="round(count(Item) div (count(ColumnBreak) + 1))" />
    <table>
      <xsl:for-each select="Item[position() &lt;= $rows]">
        <xsl:variable name="x" select="position() mod $rows"/>
        <tr>
            <xsl:for-each select="../Item[position() mod $rows = $x]">
              <td style="col-md-{$cols}">
                  <xsl:value-of select="."/>
              </td>
            </xsl:for-each>
        </tr>
      </xsl:for-each>
    </table>
 </xsl:template>