根据字符串的第一个字符拆分XML文件

根据字符串的第一个字符拆分XML文件

问题描述:

我想根据元素的起始字母将文件拆分为多个文件。例如:根据字符串的第一个字符拆分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代码执行这个转换?

谢谢!

使用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

如果您使用的是XSLT 2.0,请查看<xsl:result-document>

如果您使用XSLT 1.0,则需要扩展元素,例如<exsl:document>

更多的线索: