xml - 没有重复项的 xslt 组子组

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

最佳答案

我认为您只需要在此处使用两个 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"/>

请注意 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,这可以简化,因为它具有特殊的分组命令。

关于xml - 没有重复项的 xslt 组子组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12525853/

相关文章:

xml - 如何对包含数字但有时也包含字母的 XML 条目进行排序?

xml - 使用 XSLT 1.0 根据 ID 值替换 xml 文件的属性值

php - 如何一次处理多个 xpath(基于提要结构)或创建我自己的具有相同结构的提要

java - 不幸的是应用程序已停止(第一次 Activity )

python - 合并大量 XML 文件

xml - 类似于 lineinfile 的 Ansible xml 操作

java - 如何通过 XPath 在 Java 中使用 namespace 查询 XML?

ios - 应用程序崩溃并显示 <gap :config-file platform ="ios" parent ="NSLocationAlwaysUsageDescription" overwrite ="false"> in config. xml

javascript - 使用 JavaScript/XLS 函数在 XML 中包含命名空间

xml - 使用 XSLT 拆分和展平节点