假设我有这样一个文档:
<root>
<content>
<z>valZ</z>
<a>
<b>
<c>valC</c>
</b>
<b>
<c>valC</c>
</b>
</a>
<a>
<d>valD</d>
</a>
</content>
</root>
节点“a”的数量可以从 1 到某个不超过 30 的未定义数字 “b”、“c”和“d”节点的数量也可以从 0 到某个不超过 20 的未定义数字
我需要在 XQuery 中做的是获取节点“z”的值并将其复制到每个现有节点“b”,因此结构每次都将如下所示:
<root>
<content>
<z>valZ</z>
<a>
<b>
<c>valC</c>
<z>valZ</z>
</b>
<b>
<c>valC</c>
<z>valZ</z>
</b>
</a>
<a>
<d>valD</d>
<b> <!-- <b> was not present here before -->
<z>valZ</z>
</b>
</a>
</content>
</root>
如果只有一个 block “b”,我不需要创建另一个 block ,只需将“z”放入其中(如果存在多个“b”,则放入多个“b”内部)否则在每个“a”中"我需要创建一个新的。
看起来很简单?在 XQuery 以外的任何其他语言中,我都同意 - 您只需获取文档,复制“z”的值,遍历整个文档以查找每个“a”, 检查这些找到的“a”是否有“b”,如果没有则创建“b”并在“b”中创建具有复制值的新“z”。
但我很难迭代同一文档并使用 inmemupdate.xq 库更新它。这是我使用的代码片段:
declare function changeSourceForFieldZ($rootDoc as document-node()) as document-node()? {
let $value := getValueOfFieldZ($rootDoc)
return populateBLevelIfValueExists($rootDoc, $value)
};
declare %private function getValueOfFieldZ($rootDoc as document-node()) as text()? {
$rootDoc/*:root/*:content/*:z/text()
};
declare %private function populateBLevelIfValueExists($rootDoc as document-node(), $value as text()?) as document-node()? {
if(fn:exists($value)) then
addField($enrichment, $value)
else
$rootDoc
};
declare %private function addField($rootDoc as document-node(), $value as text()) as document-node()? {
if (hasBLevel($rootDoc)) then
insertNodeInBLevel($rootDoc, $value)
else if (hasALevel($rootDoc)) then
insertNodeInALevel($rootDoc, $value)
else ()
};
declare %private function hasBLevel($rootDoc as document-node()) as xs:boolean {
fn:exists($rootDoc/*:root/*:content/*:a/*:b)
};
declare %private function hasALevel($rootDoc as document-node()) as xs:boolean {
fn:exists($rootDoc/*:root/*:content/*:a)
};
declare %private function createTagWithZField($value as text()) {
<z>{$value}</z>
};
declare %private function createWholeBtagBlock($value as text()) {
(
<b>
<z>{$value}</z>
</b>
)
};
问题显然是插入方法。如前所述,我使用 mem 库,更准确地说是一个函数:
declare function mem:node-insert-child(
$parentNode as element(),
$newNode as node()*
) as node()
如果我这样写 insertNodeInBLevel 和 insertNodeInALevel:
declare %private function insertNodeInBLevel($rootDoc as document-node(), $value as text()) {
mem:node-insert-child($rootDoc/*:root/*:content/*:a/*:b, createTagWithZField($value))
};
declare %private function insertNodeInALevel($rootDoc as document-node(), $value as text()) {
mem:node-insert-child($rootDoc/*:root/*:content/*:a, createWholeBtagBlock($value))
};
它返回了 rootDoc 文档的多个副本,其中值添加到不同的地方,而不是一个文档的所有位置都添加了节点。
我尝试了很多解决方案,包括递归和循环:
declare %private function insertNodeInBLevel($rootDoc as document-node(), $value as text()) {
if(fn:exists($rootDoc/*:root/*:content/*:a/*:b)) then
let $nodes := $rootDoc/*:root/*:content/*:a/*:b
for $node at $index in $nodes
let $rootDoc := exampleInsertWithIndex($rootDoc, $value, $index)
return $rootDoc
};
declare %private function exampleInsertWithIndex($rootDoc as document-node(), $value as text(), $index) {
mem:node-insert-child($rootDoc/*:root/*:content/*:a/*:b[$index], createTagWithZField($value))
};
但是,好吧,值是不可变的,所以不能第二次保存到同一个 rootDoc 等等......知道如何解决这个问题,所以我将编辑同一文档的多个节点并只返回这个,不是它的副本?我是面向对象语言的开发人员,函数式语言对我来说很新,它们遵循不同的范式,因此我对解决方案的思考方式可能有缺陷......
最佳答案
这可以通过 typeswitch 来完成以及这些元素的一些自定义逻辑
declare function local:transform($nodes as node()*) as item()* {
for $node in $nodes
return
typeswitch($node)
case text() return $node
case comment() return $node
case processing-instruction() return $node
case attribute() return $node
case element(a) return local:transform-a($node)
case element(b) return local:transform-b($node)
default return local:identity($node)
};
declare function local:transform-a($a as element(a)) as element(a) {
element a {
local:transform($a/(@* | node())),
if(not(exists($a/b))) then
element b { root($a[1])/content/z }
else ()
}
};
declare function local:transform-b($b as element(b)) as element(b) {
element b {
local:transform($b/(@* | node())),
if(not(exists($b/z))) then
root($b[1])/content/z
else ()
}
};
declare function local:identity($node as element()*) as item()* {
element {name($node)} {($node/@*, local:transform($node/node()))}
};
关于xml - 在内存中的 XQuery 中多次编辑同一个文档节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34592166/
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时
作为我的Rails应用程序的一部分,我编写了一个小导入程序,它从我们的LDAP系统中吸取数据并将其塞入一个用户表中。不幸的是,与LDAP相关的代码在遍历我们的32K用户时泄漏了大量内存,我一直无法弄清楚如何解决这个问题。这个问题似乎在某种程度上与LDAP库有关,因为当我删除对LDAP内容的调用时,内存使用情况会很好地稳定下来。此外,不断增加的对象是Net::BER::BerIdentifiedString和Net::BER::BerIdentifiedArray,它们都是LDAP库的一部分。当我运行导入时,内存使用量最终达到超过1GB的峰值。如果问题存在,我需要找到一些方法来更正我的代
Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题
我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib
我的代码目前看起来像这样numbers=[1,2,3,4,5]defpop_threepop=[]3.times{pop有没有办法在一行中完成pop_three方法中的内容?我基本上想做类似numbers.slice(0,3)的事情,但要删除切片中的数组项。嗯...嗯,我想我刚刚意识到我可以试试slice! 最佳答案 是numbers.pop(3)或者numbers.shift(3)如果你想要另一边。 关于ruby-多次弹出/移动ruby数组,我们在StackOverflow上找到一
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何
我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为