交替应用两个xsl模板

时间:2014-09-01 22:04:25

标签: xml xslt

我有两个节点集,一个有名字,另一个有图像。我正在寻找一种方法来应用/调用模板,以便以这种方式对条目进行分组:X图像,8人,8人,Y图像,Z图像,8人,8人,...如果没有图像,人需要输出。每组总共有8个人,但我希望每组有不同数量的图像。

预期结果:http://i.imgur.com/YjGOv2B.png

我的xml输入很简单:

<persons>
   <entry>person 1</entry>
   <entry>person 2</entry>
   <entry>person 3</entry>
   <entry>person 4</entry>
   <entry>person 5</entry>
   <entry>…</entry>
   <entry>person 74</entry>
</persons>
<images>
   <entry>image 1</entry>
   <entry>image 2</entry>
   <entry>image 3</entry>
   <entry>image 4</entry>
   <entry>image 5</entry>
   <entry>…</entry>
   <entry>image 13</entry>
</images>

输出需要如下所示:

<!--Not part of the loop-->
<div class="imageGroup left">
   <img src="image1" class="imageGroup2-1">
   <img src="image2" class="imageGroup2-2">
</div>

<!--Start of the loop-->
<div class="personGroup right">
   <div class="person">person 1</div>
   <div class="person">person 2</div>
   <div class="person">person 3</div>
   <div class="person">person 4</div>
   <div class="person">person 5</div>
   <div class="person">person 6</div>
   <div class="person">person 7</div>
   <div class="person">person 8</div>
</div>
<div class="personGroup left">
   <div class="person">person 9</div>
   <div class="person">person 10</div>
   <div class="person">person 11</div>
   <div class="person">person 12</div>
   <div class="person">person 13</div>
   <div class="person">person 14</div>
   <div class="person">person 15</div>
   <div class="person">person 16</div>
</div>
<div class="imageGroup right">
   <img src="image3" class="imageGroup2-1">
   <img src="image4" class="imageGroup2-2">
</div>
<div class="imageGroup left">
   <img src="image5" class="imageGroup3-1">
   <img src="image6" class="imageGroup3-2">
   <img src="image7" class="imageGroup3-2">
</div>
<div class="personGroup right">
   <div class="person">person 17</div>
   <div class="person">person 18</div>
   <div class="person">person 19</div>
   <div class="person">person 20</div>
   <div class="person">person 21</div>
   <div class="person">person 22</div>
   <div class="person">person 23</div>
   <div class="person">person 24</div>
</div>
<div class="personGroup left">
   <div class="person">person 25</div>
   <div class="person">person 26</div>
   <div class="person">person 27</div>
   <div class="person">person 28</div>
   <div class="person">person 29</div>
   <div class="person">person 30</div>
   <div class="person">person 31</div>
   <div class="person">person 32</div>
</div>
<div class="imageGroup right">
   <img src="image8" class="imageGroup3-1">
   <img src="image9" class="imageGroup3-2">
   <img src="image10" class="imageGroup3-3">
</div>
<div class="imageGroup left">
   <img src="image11" class="imageGroup2-1">
   <img src="image12" class="imageGroup2-2">
</div>
<--End of the loop-->

<!--Next loop-->
<div class="personGroup right">
   <div class="person">person 33</div>
   <div class="person">person 34</div>
   <div class="person">person 35</div>
   <div class="person">person 36</div>
   <div class="person">person 37</div>
   <div class="person">person 38</div>
   <div class="person">person 39</div>
   <div class="person">person 40</div>
</div>
<div class="personGroup left">
   <div class="person">person 41</div>
   <div class="person">person 42</div>
   <div class="person">person 43</div>
   <div class="person">person 44</div>
   <div class="person">person 45</div>
   <div class="person">person 46</div>
   <div class="person">person 47</div>
   <div class="person">person 48</div>
</div>
<!--Only one image left over, class name is 2-x anyway-->
<div class="imageGroup right">
   <img src="image13" class="imageGroup2-1">
</div>
<!--As no images are left, the next group is a person group-->
<div class="personGroup left">
   <div class="person">person 49</div>
   <div class="person">person 50</div>
   <div class="person">person 60</div>
   <div class="person">person 70</div>
   <div class="person">person 71</div>
   <div class="person">person 72</div>
   <div class="person">person 73</div>
   <div class="person">person 74</div>
</div>

转型规则:

  • 输出按组排列。每个奇数组都有&#34;类&#34;离开,每个偶数小组班级&#34;右&#34;。
  • 第一组包含2张图片。第一组是豁免。
  • 以下模式正在重复:
  • 两组人
  • 具有2张图像的组和具有3张图像的组
  • 两组人
  • 具有3个图像的组和具有2个图像的组
  • (重复结束)
  • 每个图像都有一个类,告诉该组有多少图像以及图像在组中的位置(&#34; imageGroup3-2&#34;表示具有3个图像的组,此图像位于2的位置基)。
  • 如果图像节点集为空,则最后一个图像组可能包含较少的图像。在此之后,将只添加一组人。
  • 不会出现人员节点集在图像节点集之前运行的情况。

怎么做?应用模板和呼叫模板的组合?谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

这显然需要一些工作,但让我建议以下作为起点:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/">
    <root>
        <xsl:for-each select="data/persons/entry[position() mod 32 = 1]" >
        <xsl:variable name="group" select="floor(count(preceding-sibling::entry) div 32)" />
        <xsl:variable name="persons" select=". | following-sibling::entry" />
        <xsl:variable name="images" select="/data/images/entry[position() > 10*$group]" />

        <xsl:comment><xsl:value-of select="$group"/></xsl:comment>
            <!-- 2 images, left -->
            <div class="imageGroup left">
                <xsl:apply-templates select="$images[position() &lt;= 2]">
                    <xsl:with-param name="imageGroup" select="2"/>
                </xsl:apply-templates>
            </div>

            <!-- 8 persons, right -->
            <div class="personGroup right">
                <xsl:apply-templates select="$persons[position() &lt;= 8]"/>
            </div>
            <!-- 8 persons, left -->
            <div class="personGroup left">
                <xsl:apply-templates select="$persons[8 &lt; position() and position() &lt;= 16]"/>
            </div>

            <!-- 2 images, right -->
            <div class="imageGroup right">
                <xsl:apply-templates select="$images[2 &lt; position() and position() &lt;= 4]">
                    <xsl:with-param name="imageGroup" select="2"/>
                </xsl:apply-templates>
            </div>
            <!-- 3 images, left -->
            <div class="imageGroup left">
                <xsl:apply-templates select="$images[4 &lt; position() and position() &lt;= 7]">
                    <xsl:with-param name="imageGroup" select="3"/>
                </xsl:apply-templates>
            </div>

            <!-- 8 persons, right -->
            <div class="personGroup right">
                <xsl:apply-templates select="$persons[16 &lt; position() and position() &lt;= 24]"/>
            </div>
            <!-- 8 persons, left -->
            <div class="personGroup left">
                <xsl:apply-templates select="$persons[24 &lt; position() and position() &lt;= 32]"/>
            </div>

            <!-- 3 images, right -->
            <div class="imageGroup right">
                <xsl:apply-templates select="$images[7 &lt; position() and position() &lt;= 10]">
                    <xsl:with-param name="imageGroup" select="3"/>
                </xsl:apply-templates>
            </div>
        </xsl:for-each>
    </root>
</xsl:template>

<xsl:template match="persons/entry">
    <div class="person"><xsl:value-of select="."/></div>
</xsl:template>

<xsl:template match="images/entry">
    <xsl:param name="imageGroup"/>
    <img src="{.}" class="imageGroup{$imageGroup}-{position()}"/>
</xsl:template>

</xsl:stylesheet>

在此处查看此行动:http://xsltransform.net/jyH9rLZ/1