2016-03-24 74 views
0

我非常喜歡XSLT,我想根據XML文件中的特定屬性將XML文件拆分爲多個HTML文件。將XSLT拆分爲多個HTML文件

這裏是我的XML文件的樣本:

<article> 
<div> 
<paragraph>text text text</paragraph> 
</div> 
<div class="zoologie"> 
<def>this is a definition</def> 
<def>this is another definition</def> 
<example>this is a first example</example> 
<example>this is a second example</example> 
</div> 
</article> 

<article> 
<div> 
<paragraph>text text text</paragraph> 
</div> 
<div class="médecine"> 
<def>this is a definition</def> 
<def>this is another definition</def> 
<example>this is a first example</example> 
<example>this is a second example</example> 
</div> 
</article> 

<article> 
<div> 
<paragraph>text text text</paragraph> 
</div> 
<div class="zoologie"> 
<def>this is a definition</def> 
<def>this is another definition</def> 
<example>this is a first example</example> 
<example>this is a second example</example> 
</div> 
</article> 

我想獲得一個HTML文件組togheter具有相同屬性類的所有文章。例如,一個名爲zoologie.html的HTML文件收集包含屬性class =「zoologie」的所有文章。另一個名爲médecine.html的HTML文件收集了屬性class =「médecine」的所有文章。我已經嘗試了分割功能,但它不會工作。如果你能幫助我,我會很感激。 謝謝, 弗洛

+0

您可以使用XSLT 2.0處理器和'xsl:result-document',還是使用支持擴展的XSLT 1.0處理器來創建具有一個樣式表的多個文檔? –

+0

我可以使用XSLT 2.0處理器 – FloD

回答

0

這是一個典型的XSLT 2.0構造:

<xsl:for-each-group select="article" group-by="div/@class"> 
    <xsl:result-document href="{current-grouping-key()}.xml"> 
    <articles> 
     <xsl:copy-of select="current-group()"/> 
    </articles> 
    </xsl:result-document> 
</xsl:for-each-group> 

XSLT 1.0沒有任何內建做分組或產生多個輸出文件的能力,但一些1.0的處理器可能有供應商擴展幫助。

+0

謝謝您的回答。它實際上並沒有工作。我不知道爲什麼。對於我的目標,我不得不使用另一種方法,即選擇另一個具有我想要的值的屬性。然後我可以爲每個文件生成一個HTML文件。我有一個會議的最後期限,我不能再想一想使用分割功能的方法。再次感謝。 – FloD