簡體   English   中英

xsl-fo中的嵌套循環

[英]Nested loops in xsl-fo

    <years>
        <year yearValue="2012">
            <months>
                <month monthValue="4">
                    <projectElements>
                        <projectElement projectElementValue="756" />
                    </projectElements>
                </month>
                <month monthValue="8">
                    <projectElements>
                        <projectElement projectElementValue="12345" />
                    </projectElements>
                </month>
            </months>
        </year>
        <year yearValue="2013">
            <months>
                <month monthValue="8">
                    <projectElements>
                        <projectElement projectElementValue="ffff" />
                        <projectElement projectElementValue="12345" />
                    </projectElements>
                </month>
            </months>
        </year>
    </years>

我有一個如上所述的xml文件。 在我的.fo文件中,我想要這樣的循環:

每年的年數,每月的月數,對於projectElements中的每個projectElement

year = yearValue month = monthValue projectElement = projectElementValue

這不起作用:

<xsl:for-each select="activityExport/years/year">
<xsl:for-each select="activityExport/years/year/months/month">

我得到零結果。

這將按預期返回4個循環,但是隨后我松開了月份和年份信息:

<xsl:for-each select="activityExport/years/year/months/month/projectElements/projectElement">

謝謝你的幫助

你的第二個<xsl:for-each>具有正試圖解決一個相對XPath表達式activityExport是當前的子元素year (這是不存在的,所以他們產生什么)。

如果您更正了XPath使其看起來相對於上下文節點(即year元素),則將獲得預期的迭代次數。

然后,您可以解決第二個問題,即如何從最里面的<xsl:for-each>內部訪問yearmonth值。 以下是如何執行此操作的兩個示例:

1.)為了使用嵌套的<xsl:for-each>並能夠從外部<xsl:for-each>保留對上下文節點上下文的引用,您可以設置一個變量並從內部引用該變量嵌套的<xsl:for-each>語句:

<xsl:for-each select="years/year">
  <xsl:variable name="yearValue" select="@yearValue"/>
  <xsl:for-each select="months/month">
    <xsl:variable name="monthValue" select="@monthValue"/>
    <xsl:for-each select="projectElements/projectElement">
      <xsl:value-of select="concat('year = ', $yearValue, 
                                   ' month = ', $monthValue, 
                                   ' projectElement = ', @projectElementValue, 
                                   '&#xA;')"/>
    </xsl:for-each>
  </xsl:for-each>
</xsl:for-each>

2.)避免使用變量,並從最里面的<xsl:for-each>的上下文節點訪問祖先節點:

<xsl:for-each select="years/year">
  <xsl:for-each select="months/month">
     <xsl:for-each select="projectElements/projectElement">
         <xsl:value-of select="concat('year = ', ancestor::year/@yearValue, 
                                      ' month = ', ancestor::month/@monthValue, 
                                      ' projectlement = ', @projectElementValue, 
                                      '&#xA;')"/>
     </xsl:for-each>
  </xsl:for-each>
</xsl:for-each>

3.)使用一個<xsl:for-each>

<xsl:for-each select="years/year/
                       months/month/
                        projectElements/projectElement">
    <xsl:value-of select="concat('year = ', ancestor::year/@yearValue, 
                                 ' month = ', ancestor::month/@monthValue, 
                                 ' projectlement = ', @projectElementValue, 
                                 '&#xA;')"/>
</xsl:for-each>

您還可以消除<xsl:for-each>並使用<xsl:apply-templates>

<xsl:apply-templates select="years/year/months/month/projectElements/projectElement"/>

定義了模板

<xsl:template match="projectElement">
   <xsl:value-of select="concat('year = ', ancestor::year/@yearValue, 
                                ' month = ', ancestor::month/@monthValue, 
                                ' projectElement = ', @projectElementValue, 
                                '&#xA;')"/>
</xsl:template>

每個示例都從示例XML產生以下輸出:

year = 2012 month = 4 projectElement = 756
year = 2012 month = 8 projectElement = 12345
year = 2013 month = 8 projectElement = ffff
year = 2013 month = 8 projectElement = 12345

外循環的上下文會產生“ activityExport /年/年”,因此內循環應相對於此路徑(或月/月)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM