2016-10-06 30 views
1

我仍然是xslt.I中的初學者,它在爬行和列方面有困難。Xslt在兩個節點之後重複行(引導)

我有一個像這樣的XML ::

<Section Elem="Overview" child="4.0"> 
    <Section Elem="View 1" child="2.0"> 
    <Doc Elem="Tab 1"> 
    <Tit Elem="Code">Tit 1</Tit> 
    <Descr Elem="Descr1">Descr 1 </Descr> 
    </Doc> 
    </Section> 
    <Section Elem="View 2" child="2.0"> 
    <Doc Elem="Tab 2"> 
    <Tit Elem="Code">Tit 2</Tit> 
    <Descr Elem="Descr2">Descr 2 </Descr> 
    </Doc> 
    </Section>    
    <Section Elem="View 3" child="0.0"> 
    <Doc Elem="Tab 3"> 
    <Tit Elem="Code">Tit 3</Tit> 
    <Descr Elem="Descr3">Descr 3 </Descr> 
    </Doc> 
    </Section> 
    <Section Elem="View 4" child="0.0"> 
    <Doc Elem="Tab 4"> 
    <Tit Elem="Code">Tit 4</Tit> 
    <Descr Elem="Descr3">Descr 4 </Descr> 
    </Doc> 
    </Section> 
</Section> 

所需的輸出:

<div class="row"> 
    <div class="col-md-6"> 
     <h2>Tit 1</h2> 
    </div> 
    <div class="col-md-6"> 
     <h2>Tit 2</h2> 
    </div> 
</div> 
<div class="row"> 
<div class="col-md-6"> 
    <h2>Tit 3</h2> 
    </div> 
    <div class="col-md-6"> 
    <h2>Tit 4</h2> 
    </div> 
</div> 

我試過幾件事情(基於在計算器給出的答案,搜索),但沒有人給我的期望的輸出。 喜歡:

<xsl:template match="/"> 

<xsl:template match="Section/Section[position() mod 2 =1]"> 
    <div class="row"> 
     <xsl:apply-templates mode="proc" select=".|following- sibling::Section[not(position() > 2)]" /> 
    </div> 
    </xsl:template> 

<xsl:template match="Section" mode="proc"> 
     <div class="col-md-6">  
    <h2> <xsl:value-of select="Tit"/></h2> 
     </div> 
</xsl:template> 

    <xsl:template match="Section[not(position() mod 2 = 1)]"/> 

還是什麼也沒有工作:

<xsl:param name="pNumCols" select="3"/> 
    <xsl:template match="/"> 
<xsl:apply-templates select="/Sectie[position() mod $columns = 1]"/> 
    </xsl:template> 

    <xsl:template match="/Section">  <div class="row"> 
    <div class="col-md-6"> 
<xsl:for-each select=".|following-sibling::Section[not(position() > $pNumCols -1)]">   <h2> <xsl:value-of select="Tit"/></h2> 
</xsl:for-each>  </div> 
</div> 
</xsl:template> 

我最大的問題是,我不能經過兩層的部分重複。

+0

那麼,爲什麼這個問題被標記[標籤:CSS]? – connexo

回答

0

下面的XSLT 1.0樣式

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

    <xsl:output method="html"/> 

    <xsl:template match="/Section"> 
     <removeable> 
      <xsl:apply-templates select="Section[position() mod 2 = 1]"/> 
     </removeable> 
    </xsl:template> 

    <xsl:template match="Section"> 
     <div class="row"> 
      <xsl:apply-templates select=". | following-sibling::Section[1]" mode="proc"/> 
     </div> 
    </xsl:template> 

    <xsl:template match="Section" mode="proc"> 
     <div class="col-md-6"> 
      <h2> 
       <xsl:value-of select="Doc/Tit"/> 
      </h2> 
     </div> 
    </xsl:template> 

</xsl:stylesheet> 

結果

<removeable> 
    <div class="row"> 
    <div class="col-md-6"> 
     <h2>Tit 1</h2> 
    </div> 
    <div class="col-md-6"> 
     <h2>Tit 2</h2> 
    </div> 
    </div> 
    <div class="row"> 
    <div class="col-md-6"> 
     <h2>Tit 3</h2> 
    </div> 
    <div class="col-md-6"> 
     <h2>Tit 4</h2> 
    </div> 
    </div> 
</removeable>