xslt 프로세서가 exslt를 지원하면 set : distinct를 사용하여 고유 이벤트와 해당 이벤트가있는 고유 그룹을 반복 할 수 있습니다. xsltproc을 사용하여 다음을 테스트했지만 exslt : function을 지원하는 모든 프로세서에서 작동해야합니다. 참조 : http://www.exslt.org/func/elements/function/index.html.
여기에 게시 된 set : distinct 구현에 버그가 있음을 양지하시기 바랍니다.
test.xslt :
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:func="http://exslt.org/functions"
xmlns:set="http://exslt.org/sets"
extension-element-prefixes="func set">
<xsl:import href="set.distinct.function.xsl" />
<xsl:template match="set">
<set>
<xsl:variable name="set" select="."/>
<xsl:value-of select="count(object/tag/event)"/><xsl:text> </xsl:text>
<xsl:value-of select="count(set:distinct(object/tag/event))"/>
</xsl:for-each>
<xsl:for-each select="set:distinct(object/tag/event)">
<event>
<xsl:attribute name="name">
<xsl:value-of select="."/>
</xsl:attribute>
<xsl:variable name="event" select="." />
<xsl:for-each select="set:distinct($set/object/tag[event=$event]/group)">
<group>
<xsl:attribute name="name">
<xsl:value-of select="."/>
</xsl:attribute>
<xsl:variable name="group" select="." />
<xsl:apply-templates select="$set/object[tag/event=$event][tag/group=$group]" />
</group>
</xsl:for-each>
</event>
</xsl:for-each>
</set>
</xsl:template>
<xsl:template match="object">
<object name="{name}"/>
</xsl:template>
</xsl:stylesheet>
set.distinct.function.xsl :
가
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/functions"
xmlns:set="http://exslt.org/sets"
extension-element-prefixes="exsl"
exclude-result-prefixes="set">
<exsl:function name="set:distinct">
<xsl:param name="nodes" select="/.." />
<xsl:choose>
<xsl:when test="not($nodes)">
<exsl:result select="/.." />
</xsl:when>
<xsl:otherwise>
<xsl:variable name="distinct"
select="set:distinct($nodes[position() > 1])" />
<exsl:result select="$distinct | $nodes[1][not(. = $distinct)]" />
</xsl:otherwise>
</xsl:choose>
</exsl:function>
</xsl:stylesheet>
가 test.xml의 :
<set>
<object>
<name> Value1 </name>
<tag>
<event> Value </event>
<group> Value </group>
<other> Value </other>
</tag>
</object>
<object>
<name> Value2 </name>
<tag>
<event> Value </event>
<group> Value2 </group>
<other> Value </other>
</tag>
</object>
<object>
<name> Value3 </name>
<tag>
<event> Value </event>
<group> Value2 </group>
<other> Value </other>
</tag>
</object>
<object>
<name> Value4 </name>
<tag>
<event> Value </event>
<group> Value </group>
<other> Value </other>
</tag>
</object>
<object>
<name> Value5 </name>
<tag>
<event> Value2 </event>
<group> Value </group>
<other> Value </other>
</tag>
</object>
<object>
<name> Value6 </name>
<tag>
<event> Value2 </event>
<group> Value </group>
<other> Value </other>
</tag>
</object>
</set>
마찬가지로 및 의 값으로 변환한다 및 –
Hariz
입력 XML을 잘 작성하십시오. 형성 되었기 때문입니다. 또한 방금 만든 코드 대신 실제 코드에 관한 질문을하는 것이 훨씬 더 유용합니다. 종종 실제 문제의 작은 세부 사항이 작성된 샘플에서 제대로 재현되지 않아 대기 상태에서 질문을 변경하게되어 다른 사람들이 불완전한 질문에 시간을 낭비하게됩니다. 그냥 선생님. – Tomalak