Xslt xsl-在for each中每2个项目包装一个div

Xslt xsl-在for each中每2个项目包装一个div,xslt,Xslt,我有一个xsl:for-each,我想将每两个项目包装在一个div中。如何做到这一点 <xsl:for-each select="$datapath/PackageInfoList/PackageInfo"> <!-- lots of html in here --> </xsl:for-each> 因此,结果将是: <div> <!-- lots of html in here --> <!-- lots of htm

我有一个xsl:for-each,我想将每两个项目包装在一个div中。如何做到这一点

<xsl:for-each select="$datapath/PackageInfoList/PackageInfo">

<!-- lots of html in here -->

</xsl:for-each>

因此,结果将是:

<div>
<!-- lots of html in here -->
<!-- lots of html in here -->
</div>
<div>
<!-- lots of html in here -->
<!-- lots of html in here -->
</div>

选择奇数
元素,如下所示

<xsl:for-each select="$datapath/PackageInfoList/PackageInfo[position() mod 2 = 1]">
  <div>
     <!-- lots of html in here -->

     <!-- do something with following-sibling::PackageInfo[1] -->
  </div>
</xsl:for-each>

这适用于位置1、3、5等处的元素。手动处理相应的第一个后续


更地道

<xsl:template match="/">
  <xsl:apply-templates select="$datapath/PackageInfoList/PackageInfo" mode="group2" />
</xsl:template>

<xsl:template match="PackageInfo" mode="group2">
  <xsl:if test="position() mod 2 = 1">
    <div>
      <xsl:apply-templates select=". | following-sibling::PackageInfo[1]" />
    </div>
  </xsl:if>
</xsl:template>

<xsl:template match="PackageInfo">
  <!-- lots of html in here -->
</xsl:template>

更灵活

<xsl:template match="/">
  <xsl:apply-templates select="$datapath/PackageInfoList/PackageInfo" mode="group">
    <xsl:with-param name="groupcount" select="2" />
  </xsl:apply-templates>
</xsl:template>

<xsl:template match="PackageInfo" mode="group">
  <xsl:param name="groupcount" select="2" />

  <xsl:if test="position() mod $groupcount = 1">
    <div>
      <xsl:apply-templates select=". | following-sibling::PackageInfo[position() &lt; $groupcount]" />
    </div>
  </xsl:if>
</xsl:template>

<xsl:template match="PackageInfo">
  <!-- lots of html in here -->
</xsl:template>

选择奇数
元素,如下所示

<xsl:for-each select="$datapath/PackageInfoList/PackageInfo[position() mod 2 = 1]">
  <div>
     <!-- lots of html in here -->

     <!-- do something with following-sibling::PackageInfo[1] -->
  </div>
</xsl:for-each>

这适用于位置1、3、5等处的元素。手动处理相应的第一个后续


更地道

<xsl:template match="/">
  <xsl:apply-templates select="$datapath/PackageInfoList/PackageInfo" mode="group2" />
</xsl:template>

<xsl:template match="PackageInfo" mode="group2">
  <xsl:if test="position() mod 2 = 1">
    <div>
      <xsl:apply-templates select=". | following-sibling::PackageInfo[1]" />
    </div>
  </xsl:if>
</xsl:template>

<xsl:template match="PackageInfo">
  <!-- lots of html in here -->
</xsl:template>

更灵活

<xsl:template match="/">
  <xsl:apply-templates select="$datapath/PackageInfoList/PackageInfo" mode="group">
    <xsl:with-param name="groupcount" select="2" />
  </xsl:apply-templates>
</xsl:template>

<xsl:template match="PackageInfo" mode="group">
  <xsl:param name="groupcount" select="2" />

  <xsl:if test="position() mod $groupcount = 1">
    <div>
      <xsl:apply-templates select=". | following-sibling::PackageInfo[position() &lt; $groupcount]" />
    </div>
  </xsl:if>
</xsl:template>

<xsl:template match="PackageInfo">
  <!-- lots of html in here -->
</xsl:template>


您可能需要检查。您可能需要检查。您答案的第一部分是针对我不想要的每一个奇数元素。我如何将您的“更惯用”答案与for each结合起来?@MarkSteggles因为您没有说出您想要的内容(即通过提供“当前”和“想要的”输出样本),这是我最好的猜测。-惯用的解决方案不需要
。扔掉它,然后使用
。99%的时间
是错误的,无论如何,尽量避免使用它。你答案的第一部分是针对每一个我不想要的奇怪元素。我如何将您的“更惯用”答案与for each结合起来?@MarkSteggles因为您没有说出您想要的内容(即通过提供“当前”和“想要的”输出样本),这是我最好的猜测。-惯用的解决方案不需要
。扔掉它,然后使用
。99%的时间
都是错误的,无论如何,尽量避免使用它。