在对 xslt 进行了大约 3 小时的修补后,
我有以下输出
<?xml version="1.0" encoding="UTF-8"?>
<tops>
<topCategory name="cat1">
<top name="ninja" tuckedIn="0">
<part path="ninja_abdomen.png" bodyPart="abdomen"/>
<part path="ninja_humerus_l.png" bodyPart="humerus_l"/>
</top>
<top name="ninja" tuckedIn="0">
<part path="ninja_abdomen.png" bodyPart="abdomen"/>
<part path="ninja_humerus_l.png" bodyPart="humerus_l"/>
</top>
<top name="pirate" tuckedIn="0">
<part path="pirate_humerus_l.png" bodyPart="humerus_l"/>
</top>
</topCategory>
<topCategory name="cat2">
<top name="monk" tuckedIn="1">
<part path="monk_head.png" bodyPart="head"/>
</top>
<top name="monkey" tuckedIn="1">
<part path="monkey_thorax.png" bodyPart="thorax"/>
<part path="monkey_neck.png" bodyPart="neck"/>
</top>
<top name="monkey" tuckedIn="1">
<part path="monkey_thorax.png" bodyPart="thorax"/>
<part path="monkey_neck.png" bodyPart="neck"/>
</top>
</topCategory>
</tops>
问题是我有重复<top>
的 s 我只想<top>
为每个名称输入一个 s 条目。我相信我非常接近解决方案,但无法弄清楚。
原始的xml文件
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<tops>
<top path = "ninja_abdomen.png" bodyPart = "abdomen" name = "ninja" tuckedIn = "0" topCategory= "cat1"/>
<top path = "ninja_humerus_l.png" bodyPart = "humerus_l" name = "ninja" tuckedIn = "0" topCategory= "cat1"/>
<top path = "pirate_humerus_l.png" bodyPart = "humerus_l" name = "pirate" tuckedIn = "0" topCategory= "cat1"/>
<top path="monk_head.png" bodyPart="head" name="monk" tuckedIn="1" topCategory="cat2"/>
<top path="monkey_thorax.png" bodyPart="thorax" name="monkey" tuckedIn="1" topCategory="cat2"/>
<top path="monkey_neck.png" bodyPart="neck" name="monkey" tuckedIn="1" topCategory="cat2"/>
</tops>
和 xslt 文件
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent = "yes"/>
<xsl:key name="eachTopCategory" match="tops/top" use="@topCategory"/>
<xsl:key name="eachTopName" match="tops/top" use="@name"/>
<xsl:key name="eachTop" match="tops/top" use="concat(@topCategory,'|', @name)"/>
<xsl:key name="eachPart" match="tops/top" use="concat(@bodyPart,'|' ,@name,'|',@topCategory)"/>
<xsl:template match="tops">
<tops>
<xsl:apply-templates select="top[generate-id(.)=generate-id(key('eachTopCategory',@topCategory)[1])]"/>
</tops>
</xsl:template>
<xsl:template match="top">
<topCategory>
<xsl:attribute name="name">
<xsl:value-of select="@topCategory" />
</xsl:attribute>
<xsl:for-each select="key('eachTopCategory',@topCategory)">
<xsl:call-template name="sortTops"/>
</xsl:for-each>
</topCategory>
</xsl:template>
<xsl:template name="sortTops">
<top>
<xsl:attribute name="name">
<xsl:value-of select="@name" />
</xsl:attribute>
<xsl:attribute name="tuckedIn">
<xsl:value-of select="@tuckedIn" />
</xsl:attribute>
<xsl:for-each select="key('eachTop', concat(@topCategory,'|', @name))">
<xsl:call-template name="sortParts"/>
</xsl:for-each>
</top>
</xsl:template>
<xsl:template name="sortParts">
<part>
<xsl:attribute name="path">
<xsl:value-of select="@path" />
</xsl:attribute>
<xsl:attribute name="bodyPart">
<xsl:value-of select="@bodyPart" />
</xsl:attribute>
</part>
</xsl:template>
</xsl:stylesheet>
我的预期输出:
<?xml version="1.0" encoding="UTF-8"?>
<tops>
<topCategory name="cat1">
<top name="ninja" tuckedIn="0">
<part path="ninja_abdomen.png" bodyPart="abdomen"/>
<part path="ninja_humerus_l.png" bodyPart="humerus_l"/>
</top>
<top name="pirate" tuckedIn="0">
<part path="pirate_humerus_l.png" bodyPart="humerus_l"/>
</top>
</topCategory>
<topCategory name="cat2">
<top name="monk" tuckedIn="1">
<part path="monk_head.png" bodyPart="head"/>
</top>
<top name="monkey" tuckedIn="1">
<part path="monkey_thorax.png" bodyPart="thorax"/>
<part path="monkey_neck.png" bodyPart="neck"/>
</top>
</topCategory>
</tops>