草庐IT

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

coder 2024-07-04 原文

在对 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 - 没有重复项的 xslt 组子组的更多相关文章

  1. ruby - 难道Lua没有和Ruby的method_missing相媲美的东西吗? - 2

    我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/

  2. ruby-on-rails - 如何从 format.xml 中删除 <hash></hash> - 2

    我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为

  3. ruby-on-rails - rails 目前在重启后没有安装 - 2

    我有一个奇怪的问题:我在rvm上安装了ruby​​onrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(

  4. ruby 正则表达式 - 如何替换字符串中匹配项的第 n 个实例 - 2

    在我的应用程序中,我需要能够找到所有数字子字符串,然后扫描每个子字符串,找到第一个匹配范围(例如5到15之间)的子字符串,并将该实例替换为另一个字符串“X”。我的测试字符串s="1foo100bar10gee1"我的初始模式是1个或多个数字的任何字符串,例如,re=Regexp.new(/\d+/)matches=s.scan(re)给出["1","100","10","1"]如果我想用“X”替换第N个匹配项,并且只替换第N个匹配项,我该怎么做?例如,如果我想替换第三个匹配项“10”(匹配项[2]),我不能只说s[matches[2]]="X"因为它做了两次替换“1fooX0barXg

  5. ruby - 在没有 sass 引擎的情况下使用 sass 颜色函数 - 2

    我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re

  6. ruby-on-rails - 在 ruby​​ .gemspec 文件中,如何指定依赖项的多个版本? - 2

    我正在尝试修改当前依赖于定义为activeresource的gem:s.add_dependency"activeresource","~>3.0"为了让gem与Rails4一起工作,我需要扩展依赖关系以与activeresource的版本3或4一起工作。我不想简单地添加以下内容,因为它可能会在以后引起问题:s.add_dependency"activeresource",">=3.0"有没有办法指定可接受版本的列表?~>3.0还是~>4.0? 最佳答案 根据thedocumentation,如果你想要3到4之间的所有版本,你可以这

  7. 没有类的 Ruby 方法? - 2

    大家好!我想知道Ruby中未使用语法ClassName.method_name调用的方法是如何工作的。我头脑中的一些是puts、print、gets、chomp。可以在不使用点运算符的情况下调用这些方法。为什么是这样?他们来自哪里?我怎样才能看到这些方法的完整列表? 最佳答案 Kernel中的所有方法都可用于Object类的所有对象或从Object派生的任何类。您可以使用Kernel.instance_methods列出它们。 关于没有类的Ruby方法?,我们在StackOverflow

  8. ruby-on-rails - Rails 3,嵌套资源,没有路由匹配 [PUT] - 2

    我真的为这个而疯狂。我一直在搜索答案并尝试我找到的所有内容,包括相关问题和stackoverflow上的答案,但仍然无法正常工作。我正在使用嵌套资源,但无法使表单正常工作。我总是遇到错误,例如没有路线匹配[PUT]"/galleries/1/photos"表格在这里:/galleries/1/photos/1/edit路线.rbresources:galleriesdoresources:photosendresources:galleriesresources:photos照片Controller.rbdefnew@gallery=Gallery.find(params[:galle

  9. ruby-on-rails - 有没有办法为 CarrierWave/Fog 设置上传进度指示器? - 2

    我在Rails应用程序中使用CarrierWave/Fog将视频上传到AmazonS3。有没有办法判断上传的进度,让我可以显示上传进度如何? 最佳答案 CarrierWave和Fog本身没有这种功能;你需要一个前端uploader来显示进度。当我不得不解决这个问题时,我使用了jQueryfileupload因为我的堆栈中已经有jQuery。甚至还有apostonCarrierWaveintegration因此您只需按照那里的说明操作即可获得适用于您的应用的进度条。 关于ruby-on-r

  10. ruby - 没有类方法获取 Ruby 类名 - 2

    如何在Ruby中获取BasicObject实例的类名?例如,假设我有这个:classMyObjectSystem我怎样才能使这段代码成功?编辑:我发现Object的实例方法class被定义为returnrb_class_real(CLASS_OF(obj));。有什么方法可以从Ruby中使用它? 最佳答案 我花了一些时间研究irb并想出了这个:classBasicObjectdefclassklass=class这将为任何从BasicObject继承的对象提供一个#class您可以调用的方法。编辑评论中要求的进一步解释:假设你有对象

随机推荐