xslt组的子组没有重复

时间:2012-09-21 07:15:56

标签: xml xslt

使用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>个条目。我相信我非常接近解决方案,但无法弄明白。

原始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>

2 个答案:

答案 0 :(得分:4)

我认为你只需要在这里使用两个 xsl:key 元素来进行Muenchian分组。一个用于按“类别”分组,另一个用于按“类别”和“名称”连接分组

<xsl:key name="eachTopCategory" match="tops/top" use="@topCategory"/>
<xsl:key name="eachTop" match="tops/top" use="concat(@topCategory,'|', @name)"/>

您已经正确地将模板应用于按不同类别名称分组

<xsl:apply-templates 
   select="top[generate-id()=generate-id(key('eachTopCategory',@topCategory)[1])]" />

但是在与此匹配的模板中,您需要匹配不同的“名称”记录,但在所选类别中。这是您使用连锁密钥的地方:

<xsl:apply-templates 
   select="key('eachTopCategory',@topCategory)
      [generate-id()=generate-id(key('eachTop',concat(@topCategory,'|', @name))[1])]" 
      mode="top"/>

请注意使用模式,因为最终会有多个与顶部元素相匹配的模板。然后,在匹配名称的 top 元素的模板中,您将获得各个部分,如此

<xsl:apply-templates 
   select="key('eachTop',concat(@topCategory,'|', @name))" mode="part"/>

这是完整的XSLT

<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="eachTop" match="tops/top" use="concat(@topCategory,'|', @name)"/>

   <xsl:template match="tops">
      <tops>
         <xsl:apply-templates select="top[generate-id()=generate-id(key('eachTopCategory',@topCategory)[1])]" mode="category"/>
      </tops>
   </xsl:template>

   <xsl:template match="top" mode="category">
      <topCategory name="{@topCategory}">
         <xsl:apply-templates select="key('eachTopCategory',@topCategory)[generate-id()=generate-id(key('eachTop',concat(@topCategory,'|', @name))[1])]" mode="top"/>
      </topCategory>
   </xsl:template>

   <xsl:template match="top" mode="top">
      <top name="{@name}" tuckedIn="{@tuckedIn}">
         <xsl:apply-templates select="key('eachTop',concat(@topCategory,'|', @name))" mode="part"/>
      </top>
   </xsl:template>   

   <xsl:template match="top" mode="part">
      <part path="{@path}" bodyPart="{@bodyPart}" />
   </xsl:template>
</xsl:stylesheet>

当应用于您的示例XML时,会生成以下内容

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

请注意,如果您能够使用XSLT2.0,则可以简化此操作,因为它具有特殊的分组命令。

答案 1 :(得分:2)

很难相信这是我独立于蒂姆想出来的!它看起来像我一样的解决方案。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
<xsl:strip-space elements="*" />

<xsl:key name="eachTopCategory" match="top" use="@topCategory"/>
<xsl:key name="eachTop" match="top" use="concat(@topCategory,'|', @name,'|',@tuckedIn)"/>

<xsl:template match="/*">
  <tops>
    <xsl:apply-templates select="top[
      generate-id()=generate-id(key('eachTopCategory',@topCategory)[1])]" mode="category"/>
   </tops>
</xsl:template>

<xsl:template match="top" mode="category">
  <topCategory name="{@topCategory}">
    <xsl:apply-templates select="key('eachTopCategory',@topCategory)[
      generate-id()=generate-id(key('eachTop',concat(@topCategory,'|', @name,'|',@tuckedIn))[1])]" mode="top"/>
  </topCategory>
 </xsl:template>

<xsl:template match="top" mode="top">
  <top  name="{@name}" tuckedIn="{@tuckedIn}">
    <xsl:apply-templates select="key('eachTop',concat(@topCategory,'|', @name,'|',@tuckedIn))" mode="part" />
  </top>
 </xsl:template>

<xsl:template match="top" mode="part">
  <part path="{@path}" bodyPart="{@bodyPart}" />
 </xsl:template>

</xsl:stylesheet>