我有以下一段 XML:
<research>
<research.record>
<research.record_number>1</research.record_number>
<research.type>
<value lang="en-US">some research type</value>
</research.type>
<research.type>
<value lang="en-US">some other type of research</value>
</research.type>
<project.record>
<priref>101</priref>
<project.type>
<value lang="en-US">some type of project</value>
</project.type>
</project.record>
</research.record>
</research>
<research>
<research.record>
<research.record_number>2</research.record_number>
<research.type>
<value lang="en-US">some other type of research</value>
</research.type>
<research.type>
<value lang="en-US">a third type of research</value>
</research.type>
<project.record>
<priref>101</priref>
<project.type>
<value lang="en-US">some type of project</value>
</project.type>
</project.record>
</research.record>
</research>
<research>
<research.record>
<research.record_number>3</research.record_number>
<research.type>
<value lang="en-US">some other type of research</value>
</research.type>
<research.type>
<value lang="en-US">a fourth type</value>
</research.type>
<project.record>
<priref>201</priref>
<project.type>
<value lang="en-US">some other type of project</value>
</project.type>
</project.record>
</research.record>
</research>
<research>
... etc ...
在 XSLT 1.0 中,我使用 xsl:key 将此 XML 转换为唯一项目记录的列表。
到目前为止,还不错......
问题是:我还想为每个独特的项目记录显示独特的研究类型。
我的简化样式表显示了重复的 research.types(但不是唯一的)
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="html" indent="yes"/>
<xsl:key name="uniqueProject" match="project.record" use="priref"/>
<xsl:template match="record">
<div class="table-row">
<xsl:apply-templates select="//project.record[generate-id() = generate-id(key('uniqueProject', priref)[1])]">
<xsl:sort select="startdate"/>
</xsl:apply-templates>
</div>
</xsl:template>
<xsl:template match="project.record">
<div class="project-container">
<xsl:text>project.record </xsl:text>
<xsl:value-of select="priref"/>
<xsl:text>: </xsl:text>
<xsl:for-each select="//research/research.record/research.type[../project.record/priref = current()/priref]">
<xsl:sort select="value[@lang='en-US']" data-type="text" />
<xsl:if test="value[@lang='en-US'][not(.=preceding::research/research.record/research.type/value[@lang='en-US'][../../project.record/priref = current()/priref])]">
<xsl:value-of select="value[@lang='en-US']"/>
</xsl:if>
<xsl:if test="position()!=last()">
<xsl:text>, </xsl:text>
</xsl:if>
</xsl:for-each>
</div>
<br/>
<br/>
</xsl:template>
</xsl:stylesheet>
我想要的输出是:
project.record 101: some research type, some other type of research, a third type of research
project.record 201: some other type of research, a fourth type
希望有人能帮我解决正确的 XSLT/XPATH。 (只能使用XSLT1.0)
最佳答案
我会通过使用第二个键来解决这个问题,该键根据值及其关联的 priref 的组合对 research.type 值进行分组。
<xsl:key name="resTypeKey" match="research.type/value[@lang='en-US']"
use="concat(., '+++', ../../project.record/priref)" />
然后使用与表格行相同的 Muenchian 技巧:
<xsl:template match="project.record">
<div class="project-container">
<xsl:text>project.record </xsl:text>
<xsl:value-of select="priref"/>
<xsl:text>: </xsl:text>
<xsl:apply-templates select="
key('uniqueProject', priref)/../research.type
/value[@lang='en-US'][
generate-id() = generate-id(
key('resTypeKey', concat(., '+++', current()/priref))[1])]">
<xsl:sort select="." />
</xsl:apply-templates>
</div>
<br/>
<br/>
</xsl:template>
<xsl:template match="research.type/value">
<xsl:if test="position() > 1">, </xsl:if>
<xsl:value-of select="."/>
</xsl:template>
关于xml - XSLT1.0 : remove duplicates combined with an xsl:key,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19840404/
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
我有一个.pfx格式的证书,我需要使用ruby提取公共(public)、私有(private)和CA证书。使用shell我可以这样做:#ExtractPublicKey(askforpassword)opensslpkcs12-infile.pfx-outfile_public.pem-clcerts-nokeys#ExtractCertificateAuthorityKey(askforpassword)opensslpkcs12-infile.pfx-outfile_ca.pem-cacerts-nokeys#ExtractPrivateKey(askforpassword)o
在Ruby(或Rails)中,我们可以做到new_params=params.merge({:order=>'asc'})现在new_params是一个带有添加键:order的散列。但是是否有一行可以返回带有已删除key的散列?线路new_params=params.delete(:order)不会工作,因为delete方法返回值,仅此而已。我们必须分3步完成吗?tmp_params=paramstmp_params.delete(:order)returntmp_params有没有更好的方法?因为我想做一个new_params=(params[:order].blank?||para
response是一个散列,可能看起来像以下两种情况之一:response={'demo'=>'nil','test_01'=>'DemoData'}或response={'test'=>'DemoData','demo'=>'nil'}我想做这样的事情:ifresponse.has_key?'test_01'new_response.update(:nps_score=>response['test_01']elsenew_response.update(:nps_score=>response['test']end是否有更“Ruby”的方法来解决这个问题?也许使用||的东西运算符(
我想禁用HTTP参数的自动XML解析。但我发现命令仅适用于Rails2.x,它们都不适用于3.0:config.action_controller.param_parsers.deleteMime::XML(application.rb)ActionController::Base.param_parsers.deleteMime::XMLRails3.0中的等价物是什么? 最佳答案 根据CVE-2013-0156的最新安全公告你可以将它用于Rails3.0。3.1和3.2ActionDispatch::ParamsParser::
我正在尝试手动创建一些参数以传递给RailsController函数,为什么参数散列的键用冒号列出,例如params[:key]而不是params["key"]? 最佳答案 Rails使用ActiveSupport’sHashWithIndifferentAccess对于几乎所有来自其自身的哈希值,例如params。HashWithIndifferentAccess的行为与常规哈希相同,除了通过符号或具有相同“值”的字符串进行键访问会返回相同的哈希值。例如:h=HashWithIndifferentAccess.newh[:foo]
我正在遍历数组中的一组标签名称,我想使用构建器打印每个标签名称,而不是求助于“我认为:builder=Nokogiri::XML::Builder.newdo|xml|fortagintagsxml.tag!tag,somevalendend会这样做,但它只是创建名称为“tag”的标签,并将标签变量作为元素的文本值。有人可以帮忙吗?这个看起来应该比较简单,我刚刚在搜索引擎上找不到答案。我可能没有以正确的方式提问。 最佳答案 尝试以下操作。如果我没记错的话,我添加了一个根节点,因为Nokogiri需要一个。builder=Nokogi
这是一些奇怪的例子:#!/usr/bin/rubyrequire'rubygems'require'open-uri'require'nokogiri'print"withoutread:",Nokogiri(open('http://weblog.rubyonrails.org/')).class,"\n"print"withread:",Nokogiri(open('http://weblog.rubyonrails.org/').read).class,"\n"运行此返回:withoutread:Nokogiri::XML::Documentwithread:Nokogiri::
我遇到了未定义方法`to_key'的问题这是我的books_controller.rbclassBooksController和我的索引页如下。index.html.erb......现在当我要访问索引页面时出现如下错误。undefinedmethod`to_key'for# 最佳答案 index通常返回一个集合。事实上,您的Controller符合要求。但是,您的View试图为其定义一个表单。正如您所发现的,这不会成功。表单适用于实体,而不适用于集合。该错误在您看来以及您希望如何处理index。
我正在尝试加载SAML协议(protocol)架构(具体来说:https://www.oasis-open.org/committees/download.php/3407/oasis-sstc-saml-schema-protocol-1.1.xsd),但在执行此操作之后:schema=Nokogiri::XML::Schema(File.read('saml11_schema.xsd'))我得到这个输出:Nokogiri::XML::SyntaxErrorException:Element'{http://www.w3.org/2001/XMLSchema}element',att