每三个元素的行

时间:2012-01-05 12:57:27

标签: xml xslt xpath

我想在每个第三个元素周围都有一个容器 - 但是,不能让它工作,我试着这样做:

<xsl:when test="$type != ''">
            <xsl:variable name="query" select="$currentPage/* [@isDoc and string(umbracoNaviHide) != '1' and type = $type ]"/>
            <xsl:if test="count($query) &gt; 0">
                <section class="brochures-{$section-css-name}">
                    <xsl:choose>
                        <xsl:when test="$section-css-name = 'portrait' or region = $region">
                            <h2>Business</h2>
                        </xsl:when>
                        <xsl:when test="$section-css-name = 'landscape' or region = $region">
                            <h2>Panorama</h2>
                        </xsl:when>
                        <xsl:when test="$section-css-name = 'quadrat' or region = $region">
                            <h2>Image</h2>
                        </xsl:when>
                    </xsl:choose>
                    <xsl:if test="position() = 1 or position() mod 3 = 0 ">
                        <div class="row">
                            <div class="case-shadow">&nbsp;<xsl:comment/>
                            </div>
                            <xsl:for-each select="$query">
                                <xsl:variable name="externalUrl" select="externalUrl"/>
                                <xsl:if test="$externalUrl != ''">
                                    <article class="brochure">
                                        <a href="{$externalUrl}/MailView/" target="_blank" class="replaceURL">
                                            <xsl:if test="$type = 60">
                                                <img src="{$externalUrl}/MailView/pagedata/frontpage.jpg" width="160" height="228" alt="" class="reflect"/>
                                            </xsl:if>
                                            <xsl:if test="$type = 61">
                                                <img src="{$externalUrl}/MailView/pagedata/frontpage.jpg" width="228" height="160" alt="" class="reflect"/>
                                            </xsl:if>
                                            <xsl:if test="$type = 62">
                                                <img src="{$externalUrl}/MailView/pagedata/frontpage.jpg" width="160" height="160" alt="" class="reflect"/>
                                            </xsl:if>
                                            <p>
                                                <xsl:value-of select="headline"/>
                                            </p>
                                        </a>
                                    </article>
                                </xsl:if>
                            </xsl:for-each>
                        </div>
                    </xsl:if>
                </section>
            </xsl:if>
        </xsl:when>

我希望HTML的输出为:

<section class="brochures-portrait">
<h2>Business</h2>
<div class="row">
    <div class="case-shadow">&nbsp;<!----></div>
    <article class="brochure">
    </article>
    <article class="brochure">
    </article>
    <article class="brochure">
    </article>
</div>
<div class="row">
<div class="case-shadow">&nbsp;<!----></div>
    <article class="brochure">
    </article>
    <article class="brochure">
    </article>
</div>  
</section>

然而,我只在我的所有宣传册中得到1 :(


好的,thx,但我不太确定如何将其与此相结合:

<xsl:template match="/">
    <xsl:variable name="chosenregion">
        <xsl:value-of select="umbraco.library:RequestQueryString('region')"/>
    </xsl:variable>
    <xsl:if test="$currentPage/showRegionNavgation = '1'">
        <nav class="region-sort">
            <ul>
                <li class="grey-gradient">
                    <xsl:if test="$chosenregion = ''">
                        <xsl:attribute name="class">selected</xsl:attribute>
                    </xsl:if>
                    <a href="?">All</a>
                </li>
                <li class="grey-gradient">
                    <xsl:if test="$chosenregion = '76'">
                        <xsl:attribute name="class">selected</xsl:attribute>
                    </xsl:if>
                    <a href="?region=76">North</a>
                </li>
                <li class="grey-gradient">
                    <xsl:if test="$chosenregion = '77'">
                        <xsl:attribute name="class">selected</xsl:attribute>
                    </xsl:if>
                    <a href="?region=77">Mid</a>
                </li>
                <li class="grey-gradient last">
                    <xsl:if test="$chosenregion = '78'">
                        <xsl:attribute name="class">selected</xsl:attribute>
                    </xsl:if>
                    <a href="?region=78">South</a>
                </li>
            </ul>
        </nav>
    </xsl:if>

    <xsl:call-template name="brochure">
        <xsl:with-param name="type">60</xsl:with-param>
        <xsl:with-param name="region">
            <xsl:value-of select="umbraco.library:RequestQueryString('region')"/>
        </xsl:with-param>
        <xsl:with-param name="section-css-name">portrait</xsl:with-param>
    </xsl:call-template>
    <xsl:call-template name="brochure">
        <xsl:with-param name="type">61</xsl:with-param>
        <xsl:with-param name="region">
            <xsl:value-of select="umbraco.library:RequestQueryString('region')"/>
        </xsl:with-param>
        <xsl:with-param name="section-css-name">landscape</xsl:with-param>
    </xsl:call-template>
    <xsl:call-template name="brochure">
        <xsl:with-param name="type">62</xsl:with-param>
        <xsl:with-param name="region">
            <xsl:value-of select="umbraco.library:RequestQueryString('region')"/>
        </xsl:with-param>
        <xsl:with-param name="section-css-name">quadrat</xsl:with-param>
    </xsl:call-template>
</xsl:template>
<xsl:template name="brochure">
    <xsl:param name="type"/>
    <xsl:param name="section-css-name"/>
    <xsl:param name="region"/>
    <xsl:choose>
        <xsl:when test="$region != ''">
            <xsl:variable name="query" select="$currentPage/* [@isDoc and string(umbracoNaviHide) != '1' and type = $type and region = $region ]"/>
            <xsl:if test="count($query) &gt; 0">
                <section class="brochures-{$section-css-name}">
                    <xsl:choose>
                        <xsl:when test="$section-css-name = 'portrait' or region = $region">
                            <h2>
                                <xsl:value-of select="umbraco.library:GetDictionaryItem('Business')"/>
                            </h2>
                        </xsl:when>
                        <xsl:when test="$section-css-name = 'landscape' or region = $region">
                            <h2>
                                <xsl:value-of select="umbraco.library:GetDictionaryItem('Panorama')"/>
                            </h2>
                        </xsl:when>
                        <xsl:when test="$section-css-name = 'quadrat' or region = $region">
                            <h2>
                                <xsl:value-of select="umbraco.library:GetDictionaryItem('Image')"/>
                            </h2>
                        </xsl:when>
                    </xsl:choose>
                    <div class="case-shadow">&nbsp;<xsl:comment/>
                    </div>
                    <xsl:for-each select="$query">
                        <xsl:variable name="externalUrl" select="externalUrl"/>
                        <xsl:if test="$externalUrl != ''">
                            <article class="brochure">
                                <a href="{$externalUrl}/MailView/" target="_blank" class="replaceURL">
                                    <xsl:if test="$type = 60">
                                        <img src="{$externalUrl}/MailView/pagedata/frontpage.jpg" width="176" height="248" alt="" class="reflect"/>
                                    </xsl:if>
                                    <xsl:if test="$type = 61">
                                        <img src="{$externalUrl}/MailView/pagedata/frontpage.jpg" width="242" height="170" alt="" class="reflect"/>
                                    </xsl:if>
                                    <xsl:if test="$type = 62">
                                        <img src="{$externalUrl}/MailView/pagedata/frontpage.jpg" width="176" height="175" alt="" class="reflect"/>
                                    </xsl:if>
                                    <p>
                                        <xsl:value-of select="headline"/>
                                    </p>
                                </a>
                            </article>
                        </xsl:if>
                    </xsl:for-each>
                </section>
            </xsl:if>
        </xsl:when>
        <xsl:when test="$type != ''">
            <xsl:variable name="query" select="$currentPage/* [@isDoc and string(umbracoNaviHide) != '1' and type = $type ]"/>
            <xsl:if test="count($query) &gt; 0">
                <section class="brochures-{$section-css-name}">
                    <xsl:choose>
                        <xsl:when test="$section-css-name = 'portrait' or region = $region">
                            <h2>Business</h2>
                        </xsl:when>
                        <xsl:when test="$section-css-name = 'landscape' or region = $region">
                            <h2>Panorama</h2>
                        </xsl:when>
                        <xsl:when test="$section-css-name = 'quadrat' or region = $region">
                            <h2>Image</h2>
                        </xsl:when>
                    </xsl:choose>
                    <div class="case-shadow">&nbsp;<xsl:comment/>
                    </div>
                    <xsl:for-each select="$query">
                        <xsl:choose>
                            <xsl:when test="position() = 1 or position() mod 3 = 0">
                                <div class="row">
                                    <xsl:variable name="externalUrl" select="externalUrl"/>
                                    <xsl:if test="$externalUrl != ''">
                                        <article class="brochure">
                                            <a href="{$externalUrl}/MailView/" target="_blank" class="replaceURL">
                                                <xsl:if test="$type = 60">
                                                    <img src="{$externalUrl}/MailView/pagedata/frontpage.jpg" width="160" height="228" alt="" class="reflect"/>
                                                </xsl:if>
                                                <xsl:if test="$type = 61">
                                                    <img src="{$externalUrl}/MailView/pagedata/frontpage.jpg" width="228" height="160" alt="" class="reflect"/>
                                                </xsl:if>
                                                <xsl:if test="$type = 62">
                                                    <img src="{$externalUrl}/MailView/pagedata/frontpage.jpg" width="160" height="160" alt="" class="reflect"/>
                                                </xsl:if>
                                                <p>
                                                    <xsl:value-of select="headline"/>
                                                </p>
                                            </a>
                                        </article>
                                    </xsl:if>
                                </div>
                            </xsl:when>
                            <xsl:otherwise>
                                <xsl:variable name="externalUrl" select="externalUrl"/>
                                <xsl:if test="$externalUrl != ''">
                                    <article class="brochure">
                                        <a href="{$externalUrl}/MailView/" target="_blank" class="replaceURL">
                                            <xsl:if test="$type = 60">
                                                <img src="{$externalUrl}/MailView/pagedata/frontpage.jpg" width="160" height="228" alt="" class="reflect"/>
                                            </xsl:if>
                                            <xsl:if test="$type = 61">
                                                <img src="{$externalUrl}/MailView/pagedata/frontpage.jpg" width="228" height="160" alt="" class="reflect"/>
                                            </xsl:if>
                                            <xsl:if test="$type = 62">
                                                <img src="{$externalUrl}/MailView/pagedata/frontpage.jpg" width="160" height="160" alt="" class="reflect"/>
                                            </xsl:if>
                                            <p>
                                                <xsl:value-of select="headline"/>
                                            </p>
                                        </a>
                                    </article>
                                </xsl:if>
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:for-each>
                </section>
            </xsl:if>
        </xsl:when>
        <xsl:otherwise>
            <xsl:variable name="query" select="$currentPage/* [@isDoc and string(umbracoNaviHide) != '1' and type = $type and region = $region ]"/>
            <xsl:if test="count($query) &gt; 0">
                <section class="brochures-{$section-css-name}">
                    <xsl:choose>
                        <xsl:when test="$section-css-name = 'portrait' or region = $region">
                            <h2>Business</h2>
                        </xsl:when>
                        <xsl:when test="$section-css-name = 'landscape' or region = $region">
                            <h2>Panorama</h2>
                        </xsl:when>
                        <xsl:when test="$section-css-name = 'quadrat' or region = $region">
                            <h2>Image</h2>
                        </xsl:when>
                    </xsl:choose>
                    <div class="case-shadow">&nbsp;<xsl:comment/>
                    </div>
                    <xsl:for-each select="$query">
                        <xsl:variable name="externalUrl" select="externalUrl"/>
                        <xsl:if test="$externalUrl != ''">
                            <article class="brochure">
                                <a href="{$externalUrl}/MailView/" target="_blank" class="replaceURL">
                                    <xsl:if test="$type = 60">
                                        <img src="{$externalUrl}/MailView/pagedata/frontpage.jpg" width="176" height="248" alt="" class="reflect"/>
                                    </xsl:if>
                                    <xsl:if test="$type = 61">
                                        <img src="{$externalUrl}/MailView/pagedata/frontpage.jpg" width="242" height="170" alt="" class="reflect"/>
                                    </xsl:if>
                                    <xsl:if test="$type = 62">
                                        <img src="{$externalUrl}/MailView/pagedata/frontpage.jpg" width="176" height="175" alt="" class="reflect"/>
                                    </xsl:if>
                                    <p>
                                        <xsl:value-of select="headline"/>
                                    </p>
                                </a>
                            </article>
                        </xsl:if>
                    </xsl:for-each>
                </section>
            </xsl:if>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

1 个答案:

答案 0 :(得分:2)

不是迭代所有元素,而是依次确定每个元素的位置,而应该尝试匹配第1,第4,第7(等)位置的相关元素。

例如,假设您的输入XML如下

<brochures>
   <brochure>Brochure 1</brochure>
   <brochure>Brochure 2</brochure>
   <brochure>Brochure 3</brochure>
   <brochure>Brochure 4</brochure>
   <brochure>Brochure 5</brochure>
</brochures>

你会得到每个第三个元素

<xsl:apply-templates select="brochure[position() mod 3 = 1]" mode="first"/>

一旦定位在这些行上,您就可以写出你的div,然后匹配该组中所需的三行。

<div class="row">
   <xsl:apply-templates select="self::*|following-sibling::*[position() &lt; 3]"/>
</div>

这是完整的XSLT。注意我已经参数化了每个div的行数,使其更易于配置。

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

   <xsl:param name="rows" select="3" />

   <xsl:template match="/brochures">
      <section>
         <xsl:apply-templates select="brochure[position() mod $rows = 1]" mode="first"/>
      </section>
   </xsl:template>

   <xsl:template match="brochure" mode="first">
      <div class="row">
         <xsl:apply-templates select="self::*|following-sibling::*[position() &lt; $rows]"/>
      </div>
   </xsl:template>

   <xsl:template match="brochure">
      <article class="brochure"/>
   </xsl:template>
</xsl:stylesheet>

(注意使用模式来区分与宣传册元素相匹配的两个模板)

当应用于上面的示例XML时,将返回以下内容

<section>
  <div class="row">
    <article class="brochure" />
    <article class="brochure" />
    <article class="brochure" />
  </div>
  <div class="row">
    <article class="brochure" />
    <article class="brochure" />
   </div>
</section>

希望这能为您提供足够的信息以使其适应您自己的XML!