Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/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
Xslt 将元素列表转换为两级嵌套结构_Xslt_Xslt 1.0 - Fatal编程技术网

Xslt 将元素列表转换为两级嵌套结构

Xslt 将元素列表转换为两级嵌套结构,xslt,xslt-1.0,Xslt,Xslt 1.0,我的输入XML文档是一个简单的项目列表。项目的数量是任意的: <items> <item name="item1"/> <item name="item2"/> <item name="item3"/> ... <item name="itemX"/> </items> 您可以尝试向$cols添加+1,如下所示: <xsl:template match="*" mode="row"> <

我的输入XML文档是一个简单的项目列表。项目的数量是任意的:

<items>
  <item name="item1"/>
  <item name="item2"/>
  <item name="item3"/>
  ...
  <item name="itemX"/>
</items>

您可以尝试向$cols添加+1,如下所示:

<xsl:template match="*" mode="row">
<tr id="{@name}">
   <xsl:apply-templates select="." mode="cell"/>
   <xsl:apply-templates select="following-sibling::*[position() &lt; ($cols +1)]" mode="cell"/>
</tr>
</xsl:template>

对表格模板尝试此操作(必须限制每个表格的项目数量):



这里是一个完整的转换:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:param name="pRows" select="3"/>
 <xsl:param name="pCols" select="2"/>

 <xsl:variable name="vItemsInTable" select="$pRows*$pCols"/>

 <xsl:template match="/*">
       <xsl:apply-templates mode="table"
            select="*[position() mod $vItemsInTable =1]"/>
 </xsl:template>

 <xsl:template match="item" mode="table">
  <table>
   <xsl:apply-templates  mode="row" select=
    "(.|following-sibling::*)
       [not(position() > $vItemsInTable) and position() mod $pCols = 1]">
   </xsl:apply-templates>
  </table>
 </xsl:template>

 <xsl:template match="item" mode="row">
  <tr>
   <xsl:apply-templates select=
     ".|following-sibling::*[not(position() > $pCols -1)]"/>
  </tr>
 </xsl:template>

 <xsl:template match="item">
  <td><xsl:apply-templates select="@name"/></td>
 </xsl:template>
</xsl:stylesheet>
<table>
   <tr>
      <td>item1</td>
      <td>item2</td>
   </tr>
   <tr>
      <td>item3</td>
      <td>item4</td>
   </tr>
   <tr>
      <td>item5</td>
      <td>item6</td>
   </tr>
</table>
<table>
   <tr>
      <td>item7</td>
      <td>item8</td>
   </tr>
   <tr>
      <td>item9</td>
      <td>item10</td>
   </tr>
   <tr>
      <td>item11</td>
      <td>item12</td>
   </tr>
</table>
<table>
   <tr>
      <td>itemX</td>
   </tr>
</table>

当此转换应用于以下XML文档时(所提供的文档已扩展为更具挑战性):


...
...
...
...
...
...
生成所需的正确结果

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:param name="pRows" select="3"/>
 <xsl:param name="pCols" select="2"/>

 <xsl:variable name="vItemsInTable" select="$pRows*$pCols"/>

 <xsl:template match="/*">
       <xsl:apply-templates mode="table"
            select="*[position() mod $vItemsInTable =1]"/>
 </xsl:template>

 <xsl:template match="item" mode="table">
  <table>
   <xsl:apply-templates  mode="row" select=
    "(.|following-sibling::*)
       [not(position() > $vItemsInTable) and position() mod $pCols = 1]">
   </xsl:apply-templates>
  </table>
 </xsl:template>

 <xsl:template match="item" mode="row">
  <tr>
   <xsl:apply-templates select=
     ".|following-sibling::*[not(position() > $pCols -1)]"/>
  </tr>
 </xsl:template>

 <xsl:template match="item">
  <td><xsl:apply-templates select="@name"/></td>
 </xsl:template>
</xsl:stylesheet>
<table>
   <tr>
      <td>item1</td>
      <td>item2</td>
   </tr>
   <tr>
      <td>item3</td>
      <td>item4</td>
   </tr>
   <tr>
      <td>item5</td>
      <td>item6</td>
   </tr>
</table>
<table>
   <tr>
      <td>item7</td>
      <td>item8</td>
   </tr>
   <tr>
      <td>item9</td>
      <td>item10</td>
   </tr>
   <tr>
      <td>item11</td>
      <td>item12</td>
   </tr>
</table>
<table>
   <tr>
      <td>itemX</td>
   </tr>
</table>

项目1
项目2
项目3
项目4
项目5
项目6
项目7
项目8
项目9
项目10
项目11
项目12
项目X

你能用xslt 2.0代替1.0吗?不幸的是不能,至少不需要很多工作。
+1
给了我更多的副本,但是有了
-1
,现在就很接近了。对于13个项目,rows=3和cols=4,我得到最后一个项目(
item13
)作为第一个
上不需要的行,除了它自己的表之外:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:param name="pRows" select="3"/>
 <xsl:param name="pCols" select="2"/>

 <xsl:variable name="vItemsInTable" select="$pRows*$pCols"/>

 <xsl:template match="/*">
       <xsl:apply-templates mode="table"
            select="*[position() mod $vItemsInTable =1]"/>
 </xsl:template>

 <xsl:template match="item" mode="table">
  <table>
   <xsl:apply-templates  mode="row" select=
    "(.|following-sibling::*)
       [not(position() > $vItemsInTable) and position() mod $pCols = 1]">
   </xsl:apply-templates>
  </table>
 </xsl:template>

 <xsl:template match="item" mode="row">
  <tr>
   <xsl:apply-templates select=
     ".|following-sibling::*[not(position() > $pCols -1)]"/>
  </tr>
 </xsl:template>

 <xsl:template match="item">
  <td><xsl:apply-templates select="@name"/></td>
 </xsl:template>
</xsl:stylesheet>
<items>
    <item name="item1"/>
    <item name="item2"/>
    <item name="item3"/>   ...
    <item name="item4"/>
    <item name="item5"/>
    <item name="item6"/>   ...
    <item name="item7"/>
    <item name="item8"/>
    <item name="item9"/>   ...
    <item name="item10"/>   ...
    <item name="item11"/>   ...
    <item name="item12"/>   ...
    <item name="itemX"/>
</items>
<table>
   <tr>
      <td>item1</td>
      <td>item2</td>
   </tr>
   <tr>
      <td>item3</td>
      <td>item4</td>
   </tr>
   <tr>
      <td>item5</td>
      <td>item6</td>
   </tr>
</table>
<table>
   <tr>
      <td>item7</td>
      <td>item8</td>
   </tr>
   <tr>
      <td>item9</td>
      <td>item10</td>
   </tr>
   <tr>
      <td>item11</td>
      <td>item12</td>
   </tr>
</table>
<table>
   <tr>
      <td>itemX</td>
   </tr>
</table>