XSLT For-Each包装div中的每个第n项

时间:2012-03-28 13:25:29

标签: xslt umbraco

我有一系列节点,它们是父节点的直接子节点,我想在这些节点上循环,但是将它们包含在4个“组”中...我可能没有非常清楚地写这个,所以这可能会有所帮助;

<span class="child01">@nodename</span>
<span class="child02">@nodename</span>
<span class="child03">@nodename</span>
<span class="child04">@nodename</span>
<span class="child05">@nodename</span>
<span class="child06">@nodename</span>
<span class="child07">@nodename</span>
<span class="child08">@nodename</span>
..
<span class="child32">@nodename</span>
<span class="child33">@nodename</span>
..and so on

目标

<div class="group">
<span class="child01">@nodename</span>
<span class="child02">@nodename</span>
<span class="child03">@nodename</span>
<span class="child04">@nodename</span>
</div>
<div class="group">
<span class="child05">@nodename</span>
<span class="child06">@nodename</span>
<span class="child07">@nodename</span>
<span class="child08">@nodename</span>
</div>
<div class="group">
..
<span class="child32">@nodename</span>
</div>
<div class="group">
<span class="child33">@nodename</span>
..and so on

我尝试过这个想法的变体 - 在开放和关闭组标签中包装该批次,并在新的关闭/打开对中每隔四个循环下降

<div class="group">
<xsl:for-each select="$currentPage/*">

<span>
<xsl:value-of select="@nodeName" />
</span>

 <!--
            =============================================================
            After very 4th item  
            =============================================================
            -->
            <xsl:if test="position() mod 4 = 0">
              <xsl:text></div><div class="page"></xsl:text>
            </xsl:if>

        </xsl:for-each>
</div>

但基本上似乎XSLT不会让我从关闭不匹配的标签开始 我到目前为止找到的clkoset解决方案是jquery Wrapping a div around every three divs中的“修复”,但我宁愿不依赖于javascript来格式化页面。

3 个答案:

答案 0 :(得分:8)

此转化

<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="pNumCols" select="3"/>

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

 <xsl:template match="span">
  <div>
   <xsl:copy-of select=
    ".|following-sibling::span[not(position() > $pNumCols -1)]"/>
  </div>
 </xsl:template>
</xsl:stylesheet>

应用于提供的XML文档

<t>
    <span class="child01">@nodename</span>
    <span class="child02">@nodename</span>
    <span class="child03">@nodename</span>
    <span class="child04">@nodename</span>
    <span class="child05">@nodename</span>
    <span class="child06">@nodename</span>
    <span class="child07">@nodename</span>
    <span class="child08">@nodename</span> .. 
    <span class="child32">@nodename</span>
    <span class="child33">@nodename</span>
</t>

产生想要的结果

<div>
   <span class="child01">@nodename</span>
   <span class="child02">@nodename</span>
   <span class="child03">@nodename</span>
</div>
<div>
   <span class="child04">@nodename</span>
   <span class="child05">@nodename</span>
   <span class="child06">@nodename</span>
</div>
<div>
   <span class="child07">@nodename</span>
   <span class="child08">@nodename</span>
   <span class="child32">@nodename</span>
</div>
<div>
   <span class="child33">@nodename</span>
</div>

答案 1 :(得分:0)

如果像我一样你需要转换按位置划分的源元素,请使用xsl:for-each而不是xsl:copy:

 <xsl:template match="span">
   <ol>
     <xsl:for-each select=".|following-sibling::span[not(position() > $pNumCols -1)]"/>
       <li><xsl:value-of select="./text()"/></li>
     </xsl:for-each>
   </ol>
</xsl:template>

答案 2 :(得分:0)

面对同样的问题,那就是想要输出

<div class="container">
  <div class="row">
    <div class="col">...</div>
    <div class="col"/>...</div>
  </div>
  <div class="row">
  ...
  </div>
</div>

来自CXML(Collection XML)文件(http://gallery.clipflair.net/collection/activities.cxml - PivotViewer显示在http://gallery.clipflair.net/activity后面的数据)

根据此处的其他建议,我创造了以下内容,但使用&#34;模式&#34;属性&#34;模板&#34;和&#34; apply-templates&#34; XSL标签改为使我更清洁我相信:

<?xml version="1.0" encoding="UTF-8"?>
<?altova_samplexml http://gallery.clipflair.net/collection/activities.cxml?>

<xsl:stylesheet 
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:cxml="http://schemas.microsoft.com/collection/metadata/2009"
  exclude-result-prefixes="cxml"
  >

  <xsl:output method="html" version="4.0" encoding="UTF-8" indent="yes"/>
  <xsl:param name="COLUMNS" select="2"/>

  <!-- ########################### -->

  <xsl:template match="/">
    <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title>ClipFlair Activities</title>
        <link rel="stylesheet" type="text/css" href="style.css"/>
      </head>
      <body>
        <xsl:apply-templates/>
      </body>
    </html>
  </xsl:template>

  <!-- ########################### -->

  <xsl:template match="cxml:Collection">
    <div class="container">
      <xsl:apply-templates/>
    </div>
  </xsl:template>

  <!-- ########################### -->

  <xsl:template match="cxml:Items">
    <xsl:apply-templates select="cxml:Item[position() mod $COLUMNS = 1]" mode="row"/>
  </xsl:template>

  <!-- ########################### -->

  <xsl:template match="cxml:Item" mode="row">
    <div class="row">
      <div>----------</div>
      <xsl:apply-templates select=".|following-sibling::cxml:Item[position() &lt; $COLUMNS]" mode="col"/>
    </div>
  </xsl:template>

  <xsl:template match="cxml:Item" mode="col">
    <xsl:variable name="URL" select="@Href"/>
    <xsl:variable name="FILENAME" select="cxml:Facets/cxml:Facet[@Name='Filename']/cxml:String/@Value"/>
    <div class="col">
      <xsl:value-of select="$FILENAME"/>  ---  <xsl:value-of select="$URL"/>
    </div>
  </xsl:template>

  <!-- ########################### -->

  <xsl:template match="*">
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="text()|@*">
  </xsl:template>

</xsl:stylesheet>

在Altova XMLSpy工具中运行时的上述输出(注意它使用altova_samplexml处理器指令来查找XML数据)是:


2DaysInParis-OpenActivity-CapRev-FR-EN.clipflair --- http://studio.clipflair.net/?activity=2DaysInParis-OpenActivity-CapRev-FR-EN.clipflair

Abu_Dukhan-CapRev-A1-AR.clipflair --- http://studio.clipflair.net/?activity=Abu_Dukhan-CapRev-A1-AR.clipflair


----------

AFarewellToArms-RevCap-C2-EN.clipflair --- http://studio.clipflair.net/?activity=AFarewellToArms-RevCap-C2-EN.clipflair

agComhaireamhCountingRND.clipflair --- http://studio.clipflair.net/?activity=agComhaireamhCountingRND.clipflair


----------

Al-imtihan-CapRev-B1-AR.clipflair --- http://studio.clipflair.net/?activity=Al-imtihan-CapRev-B1-AR.clipflair

AlBar-Cap-B1-B2-IT.clipflair --- http://studio.clipflair.net/?activity=AlBar-Cap-B1-B2-IT.clipflair

...