我有一个分组变换,我想在其中包含一个计数/增量。如果分配了一个分组,则变换分组值。这些值是转换用来连接在一起的文件位置,因此内容作为一个输出。如果没有分配分组,则转换使用该值来获取文件内容而不将其附加到任何内容。
XSLT:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:key name="modules" match="module[@group]" use="concat(generate-id(..), '|', @group)"/>
<xsl:template match="root">
<AllSections>
<xsl:apply-templates />
</AllSections>
</xsl:template>
<!-- NON GROUPED PART -->
<xsl:template match="module[not(@group)]">
<page>
<content>
<xsl:variable name="var">
<xsl:value-of select="comp"/>
</xsl:variable>
<xsl:copy-of select="document(concat('../myfile/', string($var)))"/>
</content>
</page>
</xsl:template>
<!--GROUPED PART -->
<xsl:template match="module[@group][generate-id() = generate-id(key('modules', concat(generate-id(..), '|', @group))[1])]">
<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>
</page>
</xsl:template>
<xsl:template match="module"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
输入:
<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>
谢谢!