Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 没有重复项的组的xslt子组_Xml_Xslt - Fatal编程技术网

Xml 没有重复项的组的xslt子组

Xml 没有重复项的组的xslt子组,xml,xslt,Xml,Xslt,经过大约3个小时的xslt修补 我有以下输出 <?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="ni

经过大约3个小时的xslt修补

我有以下输出

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

问题是我有重复的
s,我只希望每个名称有一个
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"/>

请注意模式的使用,因为您将得到多个匹配顶部元素的模板。然后,在匹配名称的这些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,这可以简化,因为它有特殊的分组命令。

很难相信这是我独立于Tim提出的!在我看来,这是同样的解决方案

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


如果有人想知道为什么原始xml文件如此扁平,那是因为它是从excel导出的,excel无法处理列表列表。另外,我正在使用java和XSLT1.0进行转换,因此我更喜欢使用XSLT1.0的解决方案。请列出您的预期输出好吗?如果您使用的是Java,那么在Saxon中使用XSLT2.0(社区版或商业版)应该没有问题。使用Saxon有什么问题?需要安装Saxon。我希望我的剧本和类似的东西更轻便,蒂姆!你比我的帖子快了一分钟,我对你有一个完全相同的解决方案,一直到模式名称!除了对
@tuckedIn
属性的处理。由于该属性在每个
'eachTop'
组中发出一次,因此我认为该属性应该包含在该级别的分组表达式中是有意义的。是的,如果两个“名称”可能具有不同的“tuckedIn”值,则需要您的解决方案。我的解决方案中假设没有。