2011-01-13 137 views
1

我想根據元素的起始字母將文件拆分爲多個文件。例如:根據字符串的第一個字符拆分XML文件

<Employees>  
<Employee id="1"> 
<firstname value="Atif"></firstname>   
<lastname value="Bashir"></lastname>   
<age >32</age>   
</Employee>  
<Employee id="2"> 
<firstname value="xyz"></firstname>   
<lastname value="abc"></lastname>   
<age >32</age>   
</Employee>  
<Employee id="3"> 
<firstname value="abc"></firstname>   
<lastname value="none"></lastname>   
<age >32</age>   
</Employee>  
</Employees> 

施加變換後,將上述文件應該被分成兩個文件,因爲僱員/姓名[@value](和組中的所有數據)的第一個字符。因此,對於上述情況下的第一個文件應該是:

A.XML

<Employees>  
<Employee id="1"> 
<firstname value="Atif"></firstname>   
<lastname value="Bashir"></lastname>   
<age >32</age>   
</Employee>  
<Employee id="3"> 
<firstname value="abc"></firstname>   
<lastname value="none"></lastname>   
<age >32</age>   
</Employee>  
</Employees> 

和第二個文件應該是:

x.xml

<Employees>  
<Employee id="2"> 
<firstname value="xyz"></firstname>   
<lastname value="abc"></lastname>   
<age >32</age>   
</Employee>  
</Employees>  

什麼是XSLT代碼執行這個轉換?

謝謝!

回答

3

使用XSLT 2.0:

<xsl:for-each-group select="Employee" 
        group-by="lower-case(substring(firstname,1,1))"> 
    <xsl:result-document href="{current-grouping-key()}.xml"> 
    <xsl:copy-of select="current-group()"/> 
    </xsl:result-document> 
</xsl:for-each-group> 
+0

+1優秀Kay博士。我添加了一點格式,所以它不滾動... – 2011-01-13 23:18:21