입력 :그룹화의 그룹화 된 데이터
<persons>
<person name="John" role="Writer"/>
<person name="John" role="Poet"/>
<person name="Jacob" role="Writer"/>
<person name="Jacob" role="Poet"/>
<person name="Joe" role="Poet"/>
</persons>
예상 출력 :
<groups>
<group roles="Wriet, Poet" persons="John, Jacob"/>
<group roles="Poet" persons="Joe"/>
</groups>
위의 예에서와 같이, 나는 첫 번째 사람의 이름을 그룹에 필요 모든 사람의 역할을 찾을 수 있습니다. 두 명 이상의 사람이 동일한 역할 세트를 갖고있는 경우 (예 : John과 Jacob이 모두 작가이자 시인 인 경우) 각 역할 세트를 그룹화하고 사람 이름을 나열해야합니다.
I 등 Muenchian 방법 또는 EXSLT set:distinct
을 사용하여 그룹핑의 제 수준이 할 수
<groups>
<group roles="Wriet, Poet" persons="John"/>
<group roles="Wriet, Poet" persons="Jacob"/>
<group roles="Poet" persons="Joe"/>
</groups>
:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:sets="http://exslt.org/sets" extension-element-prefixes="sets">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:key name="persons-by-name" match="person" use="@name"/>
<xsl:template match="persons">
<groups>
<xsl:for-each select="sets:distinct(person/@name)">
<group>
<xsl:attribute name="persons"><xsl:value-of select="."/></xsl:attribute>
<xsl:attribute name="roles">
<xsl:for-each select="key('persons-by-name', .)">
<xsl:value-of select="@role"/>
<xsl:if test="position()!=last()"><xsl:text>, </xsl:text></xsl:if>
</xsl:for-each>
</xsl:attribute>
</group>
</xsl:for-each>
</groups>
</xsl:template>
</xsl:stylesheet>
하지만, I가 필요 그룹화 된 역할을 그룹화하는 방법을 이해하는 데 도움이됩니다.
XSLT 1.0 솔루션을 사용할 수없는 경우 XSLT 2.0 접근 방식을 적극 권장하십시오.
당신은 XSLT를 표시 할 수 있습니다 현재 출력을 얻으려고 현재 사용하고 있습니까? 감사! –
@ TimC 물론, 내 질문에 EXSLT 버전을 추가했습니다. – Somu