我正在處理一個樣式表,該樣式表從輸入文件以幾乎沒有分層格式輸出。每個部分都有一個非常平坦的層次結構,所以我一直在使用一種建議給我的分組方法 - 它將每個集合按第一個節點名稱進行分組,從而在平面部分創建出一個很好的層次結構。這種方法效果很好 - 我只需要修改它以說明我想跳過的元素。XSLT 2.0 - 使用分組嵌套元素
樣品輸入文件(注意:有每節的多個頭元件):
<Root>
<VolumeName>Volume 1 </VolumeName>
<Section>
<SectionName> Section1 </SectionName>
<Title> Title1 </Title>
<Header> NameOfDocument1 </Header>
<Header> Header1 </Header>
<Sub1> Sub1 first </Sub1>
<Sub1> Sub1 second </Sub1>
<Sub2> Sub2 first, Sub1 second </Sub2>
<Sub1> Sub1 third </Sub1>
<Sub2> Sub2 first, Sub1 third </Sub2>
</Section>
<Section>
<SectionName> Section2 </SectionName>
<Title> Title2 </Title>
<Header> Header2 </Header>
<Sub1> Sub1 first </Sub1>
<Sub1> Sub1 second </Sub1>
<Sub2> Sub2 first, Sub1 second </Sub2>
<Sub1> Sub1 third </Sub1>
<Sub2> Sub2 first, Sub1 third </Sub2>
</Section>
</Root>
樣品輸入代碼的輸出應爲:
<Volume1>
<Section1 Number="Section1" Name="NameOfDocument1" Title="Title1">
<Header1>
<Step>
Sub1 first
</Step>
<Step>
Sub1 second
<Step>
Sub2 first, Sub1 second
</Step>
</Step>
<Step>
Sub1 third
<Step>
Sub2 first, Sub1 third
</Step>
</Step>
</Header1>
</Section1>
<Section2 Number="Section2" Name="concat('NameOfDocument','2')" Title="Title2">
<Step>
Sub1 first
</Step>
<Step>
Sub1 second
<Step>
Sub2 first, Sub1 second
</Step>
</Step>
<Step>
Sub1 third
<Step>
Sub2 first, Sub1 third
</Step>
</Step>
</Section2>
</Volume1>
由於迪米特里Novatchev,我現在有一些代碼可以處理Section元素中的平坦部分。我有一個與Section元素相匹配的模板,然後我聲明一個元素並從SectionName,Title和Header中獲取信息,以填充元素將被調用的內容及其屬性。我想跳過SectionName,Title,有時候標題,但我不確定如何修改Dimitrie的代碼。任何建議將不勝感激!謝謝!
迪米特里的分組碼:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/*">
<Root>
<xsl:apply-templates select="*[1]">
<xsl:with-param name="pScope" select="*"/>
<xsl:with-param name="pElemName" select="name(*[1])"/>
</xsl:apply-templates>
</Root>
</xsl:template>
<xsl:template match="*">
<xsl:param name="pScope"/>
<xsl:param name="pElemName" select="'Step'"/>
<xsl:for-each-group select="$pScope"
group-starting-with="*[name()= name($pScope[1])]">
<xsl:element name="{$pElemName}">
<xsl:value-of select="."/>
<xsl:apply-templates select="current-group()[2]">
<xsl:with-param name="pScope"
select="current-group()[position() > 1]"/>
</xsl:apply-templates>
</xsl:element>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>
+1除了這是一個小修改,它仍然是一個很好的問題。 – 2011-02-22 22:33:02