XSLT如何在Muenchian分组转换中递增?

XSLT如何在Muenchian分组转换中递增?,xslt,xslt-1.0,Xslt,Xslt 1.0,我有一个分组转换,我想在其中包含一个计数/增量。如果指定了分组,则变换会对值进行分组。这些值是转换用于连接在一起的文件位置,以便将内容作为一个文件输出。如果没有指定分组,转换将使用该值获取文件内容,而不将其附加到任何内容 XSLT: 输入: <root> <section> <subsection> <module> <co

我有一个分组转换,我想在其中包含一个计数/增量。如果指定了分组,则变换会对值进行分组。这些值是转换用于连接在一起的文件位置,以便将内容作为一个文件输出。如果没有指定分组,转换将使用该值获取文件内容,而不将其附加到任何内容

XSLT:

输入:

    <root>
        <section>
            <subsection>
                <module>
                    <comp>aaa.html</comp>
                </module>
                <module group='group01'>
                    <comp>bbb.html</comp>
                </module>
                <module group='group01'>
                    <comp>ccc.html</comp>
                </module>
                <module>
                    <comp>ddd.html</comp>
                </module>
                <module>
                    <comp>eee.html</comp>
                </module>
            </subsection>
        </section>
        <section>
            <subsection>
                <module group ="group02">
                    <comp>fff.html</comp>
                </module>
                <module group ="group02">
                    <comp>ggg.html</comp>
                </module>
                <module>
                    <comp>hhh.html</comp>
                </module>
                <module group ="group03">
                    <comp>iii.html</comp>
                </module>
                <module group ="group03">
                    <comp>jjj.html</comp>
                </module>
            </subsection>
            <subsection>
                <module group ="group04">
                    <comp>kkk.html</comp>
                </module>
                <module group ="group04">
                    <comp>lll.html</comp>
                </module>
                <module group ="group05">
                    <comp>mmm.html</comp>
                </module>
                <module group ="group05">
                    <comp>nnn.html</comp>
                </module>
                <module group ="group06">
                    <comp>ooo.html</comp>
                </module>
                <module group ="group06">
                    <comp>ppp.html/comp>
                </module>
                <module>
                    <comp>qqq.html</comp>
                </module>
            </subsection>
        </section>
    </root>
输出样本:

    <?xml version="1.0" encoding="utf-8"?>
    <AllSections>   
        <section>        
            <subsection>            
                <page>
                    <content>
                        CONTENT FROM aaa.html
                    </content>
                </page>            
                <page>
                    <content>
                        CONTENT FROM bbb.html
                        CONTENT FROM ccc.html
                    </content>
                </page>           
                <page>
                    <content>
                        CONTENT FROM ddd.html
                    </content>
                </page>           
                <page>
                    <content>
                        CONTENT FROM eee.html
                    </content>
                </page>            
            </subsection>       
        </section>    
        <section>       
            <subsection>  
                <page>
                    <content>
                        CONTENT FROM fff.html
                        CONTENT FROM ggg.html
                    </content>
                </page>           
                <page>
                    <content>
                        CONTENT FROM hhh.html
                    </content>
                </page>            
                <page>
                    <content>
                        CONTENT FROM iii.html
                        CONTENT FROM jjj.html
                    </content>
                </page>         
            </subsection>        
            <subsection>            
                <page>
                    <content>
                        CONTENT FROM kkk.html
                        CONTENT FROM lll.html
                    </content>
                </page>           
                <page>
                    <content>
                        CONTENT FROM mmm.html
                        CONTENT FROM nnn.html
                    </content>
                </page>           
                <page>
                    <content>
                        CONTENT FROM ooo.html
                        CONTENT FROM ppp.html
                    </content>
                </page>           
                <page>
                    <content>
                        CONTENT FROM qqq.html
                    </content>
                </page>           
            </subsection>       
        </section>    
    </AllSections>
我试图做的是为总页面设置一个计数器,见下文,关于小节和小节,并将增量分配给。我尝试过param,但它需要递归模板?如果我加入sem来破坏其余的转换。使用模板时是否已创建“循环”

正在尝试获取的输出:

   <AllSections>   
        <section>        
            <subsection>            
                <page>
                    <content>
                        CONTENT FROM aaa.html
                    </content>
                    <page_no>1</page_no>
                </page>            
                <page>
                    <content>
                        CONTENT FROM bbb.html
                        CONTENT FROM ccc.html
                    </content>
                    <page_no>2</page_no>
                </page>           
                <page>
                    <content>
                        CONTENT FROM ddd.html
                    </content>
                    <page_no>3</page_no>
                </page>           
                <page>
                    <content>
                        CONTENT FROM eee.html
                    </content>
                    <page_no>4</page_no>
                </page>                   
                <subscection_total_pages>4</subscection_total_pages>
            </subsection>  
            <scection_total_pages>4</scection_total_pages>
        </section>    
        <section>       
            <subsection>  
                <page>
                    <content>
                        CONTENT FROM fff.html
                        CONTENT FROM ggg.html
                    </content>
                    <page_no>1</page_no>
                </page>           
                <page>
                    <content>
                        CONTENT FROM hhh.html
                    </content>
                    <page_no>2</page_no>
                </page>            
                <page>
                    <content>
                        CONTENT FROM iii.html
                        CONTENT FROM jjj.html
                    </content>
                    <page_no>3</page_no>
                </page>
                <subscection_total_pages>3</subscection_total_pages>
            </subsection>        
            <subsection>            
                <page>
                    <content>
                        CONTENT FROM kkk.html
                        CONTENT FROM lll.html
                    </content>
                    <page_no>1</page_no>
                </page>           
                <page>
                    <content>
                        CONTENT FROM mmm.html
                        CONTENT FROM nnn.html
                    </content>
                    <page_no>2</page_no>
                </page>           
                <page>
                    <content>
                        CONTENT FROM ooo.html
                        CONTENT FROM ppp.html
                    </content>
                    <page_no>3</page_no>
                </page>           
                <page>
                    <content>
                        CONTENT FROM qqq.html
                    </content>
                    <page_no>4</page_no>
                </page>
                <subscection_total_pages>4</subscection_total_pages>
            </subsection>
            <scection_total_pages>7</scection_total_pages>
        </section>    
    </AllSections>
谢谢

以下是我的建议:

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

       <xsl:key name="modules" match="module[@group]" use="concat(generate-id(..), '|', @group)"/>

       <xsl:template match="root">
           <AllSections>
               <xsl:apply-templates />
           </AllSections>
       </xsl:template>

       <xsl:template match="section">
         <xsl:copy>
           <xsl:apply-templates select="subsection"/>
           <section_total_pages>
             <xsl:value-of select="
             count(subsection/module[not(@group)]) + 
             count(subsection/module[@group][generate-id() = generate-id(key('modules', concat(generate-id(..), '|', @group))[1])])"/>
           </section_total_pages>
         </xsl:copy>
       </xsl:template>

       <xsl:template match="subsection">
          <xsl:copy>
            <xsl:variable name="groups" select="module[not(@group)] | module[@group][generate-id() = generate-id(key('modules', concat(generate-id(..), '|', @group))[1])]"/>
            <xsl:apply-templates select="$groups"/>
            <subsection_total_pages>
              <xsl:value-of select="count($groups)"/>
            </subsection_total_pages>
          </xsl:copy>
        </xsl:template>


       <!-- NON GROUPED PART -->
       <xsl:template match="module[not(@group)]">
              <page>
               <content>
                <xsl:copy-of select="document(concat('../myfile/', comp))"/>
               </content>
               <xsl:apply-templates select="." mode="page-no"/>
             </page>
       </xsl:template>

       <!--GROUPED PART -->
       <xsl:template match="module[@group]">
           <xsl:variable name="modules" select="key('modules', concat(generate-id(..), '|', @group))"/>
           <page>
               <content>
                <xsl:for-each select="$modules/comp">
                  <xsl:copy-of select="document(concat('../myfile/', .))"/>
                </xsl:for-each>
               </content>
               <xsl:apply-templates select="." mode="page-no"/>
           </page>
       </xsl:template>

       <xsl:template match="module" mode="page-no">
               <page_no>
                 <xsl:number count="module[not(@group)] | module[@group][generate-id() = generate-id(key('modules', concat(generate-id(..), '|', @group))[1])]"/>
               </page_no>
       </xsl:template>


       <xsl:template match="@*|node()">
           <xsl:copy>
               <xsl:apply-templates select="@*|node()"/>
           </xsl:copy>
       </xsl:template>
   </xsl:stylesheet>
当我将上述代码与Saxon 6.5.5一起应用于输入时

<root>
    <section>
        <subsection>
            <module>
                <comp>aaa.html</comp>
            </module>
            <module group='group01'>
                <comp>bbb.html</comp>
            </module>
            <module group='group01'>
                <comp>ccc.html</comp>
            </module>
            <module>
                <comp>ddd.html</comp>
            </module>
            <module>
                <comp>eee.html</comp>
            </module>
        </subsection>
    </section>
    <section>
        <subsection>
            <module group ="group02">
                <comp>fff.html</comp>
            </module>
            <module group ="group02">
                <comp>ggg.html</comp>
            </module>
            <module>
                <comp>hhh.html</comp>
            </module>
            <module group ="group03">
                <comp>iii.html</comp>
            </module>
            <module group ="group03">
                <comp>jjj.html</comp>
            </module>
        </subsection>
        <subsection>
            <module group ="group04">
                <comp>kkk.html</comp>
            </module>
            <module group ="group04">
                <comp>lll.html</comp>
            </module>
            <module group ="group05">
                <comp>mmm.html</comp>
            </module>
            <module group ="group05">
                <comp>nnn.html</comp>
            </module>
            <module group ="group06">
                <comp>ooo.html</comp>
            </module>
            <module group ="group06">
                <comp>ppp.html</comp>
            </module>
            <module>
                <comp>qqq.html</comp>
            </module>
        </subsection>
    </section>
</root>
我得到了结果

<AllSections>
   <section>
      <subsection>
         <page>
            <content/>
            <page_no>1</page_no>
         </page>
         <page>
            <content/>
            <page_no>2</page_no>
         </page>
         <page>
            <content/>
            <page_no>3</page_no>
         </page>
         <page>
            <content/>
            <page_no>4</page_no>
         </page>
         <subsection_total_pages>4</subsection_total_pages>
      </subsection>
      <section_total_pages>4</section_total_pages>
   </section>
   <section>
      <subsection>
         <page>
            <content/>
            <page_no>1</page_no>
         </page>
         <page>
            <content/>
            <page_no>2</page_no>
         </page>
         <page>
            <content/>
            <page_no>3</page_no>
         </page>
         <subsection_total_pages>3</subsection_total_pages>
      </subsection>
      <subsection>
         <page>
            <content/>
            <page_no>1</page_no>
         </page>
         <page>
            <content/>
            <page_no>2</page_no>
         </page>
         <page>
            <content/>
            <page_no>3</page_no>
         </page>
         <page>
            <content/>
            <page_no>4</page_no>
         </page>
         <subsection_total_pages>4</subsection_total_pages>
      </subsection>
      <section_total_pages>7</section_total_pages>
   </section>
</AllSections>
这似乎有正确的总数。显然,我没有要拉入的文件,因此内容都丢失了。

此XSLT 1.0样式表

替代解决方案是如何工作的? 考虑键值中使用的Xpath表达式的重要部分

如果@group不存在或为空,那么这相当于

concat(
  substring-before(
    concat( generate-id(), '@@', generate-id()),
    '@@' ),
  @group
  )
concat(
  '',
  @group
  )
。。。或者

concat( generate-id(), @group)
generate-id()
@group
。。。或者

concat( generate-id(), @group)
generate-id()
@group
。。。每个节点都是唯一的,这就是我们想要的

但是如果@group不是空的,那么这相当于

concat(
  substring-before(
    concat( generate-id(), '@@', generate-id()),
    '@@' ),
  @group
  )
concat(
  '',
  @group
  )
。。。或者

concat( generate-id(), @group)
generate-id()
@group
允许将具有group属性的模块分组在一起