在XSLT for table中为每个循环嵌套三次

时间:2015-04-13 11:58:52

标签: xml xslt

我尝试从嵌套的XML对象中获取一个表。有一些复合体。每个综合设施都有一个或多个防火隔间,每个防火隔间都有一个或多个建筑物。表输出应仅在其出现的第一行打印出每个复合体的名称。对于firecompartment也是如此。如果可能,表rowspan应该跨越相同的complex和firecompartments。

这是XML结构:

<?xml version="1.0" encoding="UTF-8"?>
 <ROOT>
  <TREE>
    ...
   <RiskArea>
   <Complex>
     <k_000 value="CO1" />
     <FireCompartment>
       <ba_000 value="FC1" />
       <Building><g_000 value="B1" /></Building>
       <Building><g_000 value="B2" /></Building>
    </FireCompartment>
    <FireCompartment>
      <ba_000 value="FC2" />
      <Building><g_000 value="B3" /></Building>
    </FireCompartment>
    ....
  </Complex>
  <Complex>
    ....
  </Complex>
  </RiskArea>
 </TREE>
</ROOT>

这是我的xslt结构:

...
<fo:table-body>
 <xsl:for-each select = "/ROOT/TREE/Group/Firm/RiskArea/Complex" >
   <xsl:variable name="complex" select="./k_000/@value"/>
   <xsl:for-each select = "./FireCompartment" >
     <xsl:for-each select = "./Building" >
         ???
         HERE INSERT CODE TO FILL UP $complex with the Name of complex for 
         the first row and with empty space for other rows for same complex 
          <xsl:call-template name="rowNormal">
            <xsl:with-param name="font-size"  select="'9pt'"/>
            <xsl:with-param name="colTexte" select="concat($complex,$COL,...)"/>
          </xsl:call-template>
     </xsl:for-each>
  </xsl:for-each>
 </xsl:for-each>
</fo:table-body>

输出应如下所示:

Complex  FireC.  Building

CO1        FC1      B1
                    B2
           FC2      B3
CO2        FC3      B4
                    B5
                    B6
 ...

1 个答案:

答案 0 :(得分:0)

您可以在层次结构中添加对每个子节点位置的一些检查,以确定是否显示该值。

试试这个XSLT

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

    <xsl:template match="/">
        <fo:table-body>
         <xsl:for-each select="/ROOT/TREE/RiskArea/Complex" >
           <xsl:variable name="complex" select="k_000/@value"/>
           <xsl:for-each select="FireCompartment" >
             <xsl:variable name="firecompartment" select="ba_000/@value" />
             <xsl:variable name="firePos" select="position()" />
             <xsl:for-each select="Building" >
                <fo:table-row>
                    <fo:table-cell>
                        <fo:block>
                        <xsl:if test="$firePos = 1 and position() = 1">
                            <xsl:value-of select="$complex" />
                        </xsl:if>
                        </fo:block>
                    </fo:table-cell>
                    <fo:table-cell>
                        <fo:block>
                        <xsl:if test="position() = 1">
                            <xsl:value-of select="$firecompartment" />
                        </xsl:if>
                        </fo:block>
                    </fo:table-cell>
                    <fo:table-cell>
                        <fo:block><xsl:value-of select="g_000/@value" /></fo:block>
                    </fo:table-cell>
                </fo:table-row>
             </xsl:for-each>
          </xsl:for-each>
         </xsl:for-each>
        </fo:table-body>
    </xsl:template>
</xsl:stylesheet>