有人可以帮我进行以下转换吗?
这里是输入的xml:
<?xml version="1.0" encoding="UTF-8"?>
<book>
<title>My book</title>
<pages>200</pages>
<size>big</size>
<author>
<name>Smith</name>
</author>
<author>
<name>Wallace</name>
</author>
<author>
<name>Brown</name>
</author>
</book>
<book>
<title>Other book</title>
<pages>100</pages>
<size>small</size>
<author>King</author>
</book>
<book>
<title>Pretty book</title>
<pages>150</pages>
<size>medium</size>
</book>
这是期望的输出
<book style="even">
<title>My book</title>
<pages>200</pages>
<size>big</size>
<author-name>Smith</author-name>
</book>
<book style="odd">
<title>My book</title>
<pages>200</pages>
<size>big</size>
<author-name>Wallace</author-name>
</book>
<book style="even">
<title>My book</title>
<pages>200</pages>
<size>big</size>
<author-name>Brown</author-name>
</book>
<book style="odd" >
<title>Other book</title>
<pages>100</pages>
<size>small</size>
<author-name>King</author-name>
</book>
<book style="even">
<title>Pretty book</title>
<pages>150</pages>
<size>medium</size>
<author-name />
</book>
我尝试使用 xsl:for-each 循环,但我想它们把我带到了死胡同。这里棘手的部分是“style”属性,无论在任何书中放置了多少作者标签,它都需要以某种方式“全局”。
最佳答案
这个简单的纯 XSLT 1.0 转换(没有条件,没有 xsl:for-each,没有参数传递,没有 xsl:element,没有使用臭名昭著的低效 //):
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:my="my:my" exclude-result-prefixes="my" >
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<my:names>
<n>odd</n>
<n>even</n>
</my:names>
<xsl:variable name="vStyles"
select="document('')/*/my:names/*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="book/author">
<xsl:variable name="vPos">
<xsl:number level="any" count="book/author|book[not(author)]"/>
</xsl:variable>
<book style="{$vStyles[$vPos mod 2 +1]}">
<xsl:copy-of select="@*|../node()[not(self::author)]"/>
<author-name>
<xsl:value-of select="normalize-space()"/>
</author-name>
</book>
</xsl:template>
<xsl:template match="book[author]">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="book[not(author)]">
<xsl:variable name="vPos">
<xsl:number level="any" count="book/author|book[not(author)]"/>
</xsl:variable>
<book style="{$vStyles[$vPos mod 2 +1]}">
<xsl:copy-of select="@*|node()"/>
<author-name/>
</book>
</xsl:template>
<xsl:template match="book[author]/*[not(self::author)]"/>
</xsl:stylesheet>
应用于此 XML 文档时(提供的包装在单个顶部元素中):
<t>
<book>
<title>My book</title>
<pages>200</pages>
<size>big</size>
<author>
<name>Smith</name>
</author>
<author>
<name>Wallace</name>
</author>
<author>
<name>Brown</name>
</author>
</book>
<book>
<title>Other book</title>
<pages>100</pages>
<size>small</size>
<author>King</author>
</book>
<book>
<title>Pretty book</title>
<pages>150</pages>
<size>medium</size>
</book>
</t>
产生完全想要的、正确的结果:
<t>
<book style="even">
<title>My book</title>
<pages>200</pages>
<size>big</size>
<author-name>Smith</author-name>
</book>
<book style="odd">
<title>My book</title>
<pages>200</pages>
<size>big</size>
<author-name>Wallace</author-name>
</book>
<book style="even">
<title>My book</title>
<pages>200</pages>
<size>big</size>
<author-name>Brown</author-name>
</book>
<book style="odd">
<title>Other book</title>
<pages>100</pages>
<size>small</size>
<author-name>King</author-name>
</book>
<book style="even">
<title>Pretty book</title>
<pages>150</pages>
<size>medium</size>
<author-name/>
</book>
</t>
说明:适当使用 xsl:number 和模板/模式匹配。
关于xml - XSLT 扁平化 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7674014/
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
我想禁用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::
我正在遍历数组中的一组标签名称,我想使用构建器打印每个标签名称,而不是求助于“我认为:builder=Nokogiri::XML::Builder.newdo|xml|fortagintagsxml.tag!tag,somevalendend会这样做,但它只是创建名称为“tag”的标签,并将标签变量作为元素的文本值。有人可以帮忙吗?这个看起来应该比较简单,我刚刚在搜索引擎上找不到答案。我可能没有以正确的方式提问。 最佳答案 尝试以下操作。如果我没记错的话,我添加了一个根节点,因为Nokogiri需要一个。builder=Nokogi
例如,如果我有YAML文件en:questions:new:'NewQuestion'other:recent:'Recent'old:'Old'这最终会变成一个json对象,例如{'questions.new':'NewQuestion','questions.other.recent':'Recent','questions.other.old':'Old'} 最佳答案 由于问题是关于在Rails应用程序上使用YAML文件进行i18n,因此值得注意i18ngem提供了一个辅助模块I18n::Backend::Flatten完全像
这是一些奇怪的例子:#!/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::
我正在尝试加载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
我正在尝试通过POST将XML内容发送到一个简单的Rails项目中的Controller(“解析”)方法(“索引”)。它不是RESTful,因为我的模型名称不同,比如“汽车”。我在有效的功能测试中有以下内容:deftest_index...data_file_path=File.dirname(__FILE__)+'/../../app/views/layouts/index.xml.erb'message=ERB.new(File.read(data_file_path))xml_result=message.result(binding)doc=REXML::Document.ne
我有这样的代码:@doc=Nokogiri::HTML(open(url)@doc.xpath(query).eachdo|html|putshtml#howgetcontentofanodeend我如何获取节点的内容而不是像这样: 最佳答案 这是READMEfile中的概要示例为Nokogiri展示了一种使用CSS、XPath或混合的方法:require'nokogiri'require'open-uri'#GetaNokogiri::HTML:Documentforthepagewe’reinterestedin...doc=N
恐怕我没有太多通过网络服务器发布文档(例如XML)的经验,所以如果我对HTTP的理解不足,我深表歉意。我在127.0.0.1上的ruby应用程序中设置了一个基本的MongrelWeb服务器端口2000.(服务器)。我在同一台计算机上运行一个单独的Ruby应用程序。(客户)。我需要客户端向服务器发送XML文档。我曾尝试使用Net::HTTP来执行此操作,但我找不到一个明确的示例来告诉我应该做什么。我试过了,但遇到了错误。我已将请求分解,使其尽可能基本:http=Net::HTTP.new("127.0.0.1",2000)http.post('file','query=foo')#x
有没有一种快速的方法可以将散列扁平化为删除键的数组?h={:at=>[10,20],:width=>100,:height=>200}结果为:[[10,20],100,200] 最佳答案 有一个内置的散列方法:h.values 关于ruby-将ruby散列扁平化为数组,并删除键,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/12863819/