2014-12-10 5 views
0

XSLT 2.0을 사용하여 XML을 변형하려고 시도합니다.플랫 XML 형제 중첩

플랫 원본 XML :

<body> 
    <R1/> 
    <R1/> 
    <R2/> 
    <R2/> 
    <R2/> 
    <R3/> 
    <R3/> 
    <R3/> 
    <R1/> 
    <R1/> 
    <R2/> 
    <R2/> 
    <R1/> 
</body> 

원하는 출력 :

<body> 
    <R1/> 
    <R1> 
    <R2/> 
    <R2/> 
    <R2> 
     <R3/> 
     <R3/> 
     <R3/> 
    </R2> 
    </R1> 
    <R1/> 
    <R1> 
    <R2/> 
    <R2/> 
    </R1> 
    <R1/> 
</body> 

기본적 이러한 R1 - R3 소자 분파-1 분파-2 분파 -3- 형 요소를 의미. R2는 이전 형제 R1에 중첩되어 있으며 R3은 이전 형제 R2 내에 중첩되어 있습니다. 동일한 요소는 같은 레벨에 있습니다.

답변

2

사용에 대한-각 그룹 그룹 시작-로는 :

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:mf="http://example.com/mf" 
    exclude-result-prefixes="xs mf" 
    version="2.0"> 

<xsl:param name="prefix" as="xs:string" select="'R'"/> 

<xsl:output indent="yes"/> 

<xsl:function name="mf:group" as="element()*"> 
    <xsl:param name="elements" as="element()*"/> 
    <xsl:param name="level" as="xs:integer"/> 
    <xsl:for-each-group select="$elements" group-starting-with="*[local-name() = concat($prefix, $level)]"> 
    <xsl:element name="{name()}"> 
     <xsl:sequence select="mf:group(current-group() except ., $level + 1)"/> 
    </xsl:element> 
    </xsl:for-each-group> 
</xsl:function> 

<xsl:template match="body"> 
    <xsl:copy> 
    <xsl:sequence select="mf:group(*, 1)"/> 
    </xsl:copy> 
</xsl:template> 

</xsl:stylesheet> 
+0

큰 일했다! 감사!!! – Laterade