草庐IT

xml - 在 XPath XSL 中执行 "Group By"查询

coder 2024-06-25 原文

给定以下 XML:

<results name="queryResults">
  <int name="intfield1:[* TO 10]">11</int> 
  <int name="intfield2:[10 TO 20]">9</int> 
  <int name="intfield1:[10 TO 20]">12</int> 
</results>

我想生成这个 XML:

<results>
    <field name="numberfield1">
        <value name="[* TO 10]">11</value>
        <value name="[10 TO 10]">12</value>
    </field>
    <field name="numberfield2">
        <value name="[10 TO 20]">9</value>
    </field>
</results>

我想不出如何在 XSL 中做到这一点,主要是因为我想按数字字段分组。我能想到的就是:

<xsl:if test="count(results/int) &gt; 0">
    <results>
    <xsl:for-each select="results/int">
        <field>
            <xsl:attribute name="name">
                <xsl:value-of select="substring-before(@name, ':')"/></xsl:attribute>
            <value>
                <xsl:attribute name="name">
                    <xsl:value-of select="substring-after(@name, ':') "/>
                </xsl:attribute>
                <xsl:value-of select="."/>
            </value>
        </field>
    </xsl:for-each>
    </results>
</xsl:if>

但是这并没有产生很好的分组列表,而是我得到了这个:

<results>
    <field name="numberfield1">
        <value name="[* TO 10]">11</value>
    </field>
    <field name="numberfield2">
        <value name="[10 TO 20]">9</value>
    </field>
    <field name="numberfield1">
        <value name="[10 TO 10]">12</value>
    </field>
</results>

如果有人能引导我朝着正确的方向前进……那该多好?

谢谢

最佳答案

要在 XSLT 1.0 中做到这一点,您必须使用一种称为 "muenchian grouping" 的技术。 .首先创建要分组的节点的键

<xsl:key name="intfield" match="int" use="substring-before(@name, ':')" />

接下来,您遍历所有节点,但只选择恰好在相关组中排在第一位的节点

<xsl:for-each select="int[generate-id() = generate-id(key('intfield', substring-before(@name, ':'))[1])]">

接下来可以迭代使用key来遍历group中的所有节点

<xsl:variable name="intfieldname" select="substring-before(@name, ':')"/>
<xsl:for-each select="key('intfield', $intfieldname)">

把这些放在一起给出

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:output method="xml"/>
   <xsl:key name="intfield" match="int" use="substring-before(@name, ':')"/>
   <xsl:template match="/results">
      <results>
         <xsl:for-each select="int[generate-id() = generate-id(key('intfield', substring-before(@name, ':'))[1])]">
            <xsl:variable name="intfieldname" select="substring-before(@name, ':')"/>
            <field>
               <xsl:attribute name="name">
                  <xsl:value-of select="$intfieldname"/>
               </xsl:attribute>
               <xsl:for-each select="key('intfield', $intfieldname)">
                  <value>
                     <xsl:attribute name="name">
                        <xsl:value-of select="substring-after(@name, ':')"/>
                     </xsl:attribute>
                     <xsl:value-of select="."/>
                  </value>
               </xsl:for-each>
            </field>
         </xsl:for-each>
      </results>
   </xsl:template>
</xsl:stylesheet>

在您的示例中,“intfield”变成了“numberfield”。在上面的示例中,我将名称保留为“intfield”。

  • 修正错别字。

关于xml - 在 XPath XSL 中执行 "Group By"查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1929092/

有关xml - 在 XPath XSL 中执行 "Group By"查询的更多相关文章

  1. ruby-on-rails - rails : "missing partial" when calling 'render' in RSpec test - 2

    我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou

  2. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  3. ruby-openid:执行发现时未设置@socket - 2

    我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass

  4. ruby - ECONNRESET (Whois::ConnectionError) - 尝试在 Ruby 中查询 Whois 时出错 - 2

    我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.

  5. 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代码修改为

  6. ruby - 检查 "command"的输出应该包含 NilClass 的意外崩溃 - 2

    为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar

  7. ruby - Chef 执行非顺序配方 - 2

    我遵循了教程http://gettingstartedwithchef.com/,第1章。我的运行list是"run_list":["recipe[apt]","recipe[phpap]"]我的phpapRecipe默认Recipeinclude_recipe"apache2"include_recipe"build-essential"include_recipe"openssl"include_recipe"mysql::client"include_recipe"mysql::server"include_recipe"php"include_recipe"php::modul

  8. ruby-on-rails - 迷你测试错误 : "NameError: uninitialized constant" - 2

    我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test

  9. ruby-on-rails - 在 Rails 和 ActiveRecord 中查询时忽略某些字段 - 2

    我知道我可以指定某些字段来使用pluck查询数据库。ids=Item.where('due_at但是我想知道,是否有一种方法可以指定我想避免从数据库查询的某些字段。某种反拔?posts=Post.where(published:true).do_not_lookup(:enormous_field) 最佳答案 Model#attribute_names应该返回列/属性数组。您可以排除其中一些并传递给pluck或select方法。像这样:posts=Post.where(published:true).select(Post.attr

  10. ruby-on-rails - 相关表上的范围为 "WHERE ... LIKE" - 2

    我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que

随机推荐