我无法理解为什么 XSLT(Saxon 9.1 和 9.5)在使用包含已排序序列的变量时的工作方式。
这是我的程序的输出,其中嵌入了我的三个问题:
<?xml version="1.0" encoding="UTF-8"?>
The following sequence is unsorted...
sequence $list = (<contribution e="4"/><contribution e="1"/><contribution e="2"/><contribution e="8"/>)
It is, as I expected, in document order.
The following sequence is explicitly sorted...
sequence $sorted-list = (<contribution e="8"/><contribution e="4"/><contribution e="2"/><contribution e="1"/>)
It is, as I expected, sorted in descending numerical @e order.
In the following output, I expect for the @e values from $list to be in document order,
and for the @e values from $sorted-list to be in descending numerical order...
value-of $list/@e = (4 1 2 8)
value-of $sorted-list/@e = (4 1 2 8)
But both are in document order.
Specifically, the $sorted-list/@e values are NOT listed in descending numerical @e order.
(Question 1: ...By the way, why can't I use 'xsl:sequence select="$list/@e"' here?)
Next, here's the real work that I'm interested in. It is a function that computes a running
subtotal of the elements passed in. Order is critical here. I expect for my sorted list to provide
@e values to the function in the order that I explicitly put them in the definition of $sorted-list.
So, in the following call, I send in the sequence that I think SHOULD have been sorted...
sequence f($sorted-list/@e) = (0 4 5 7 15)
But my result set is a list of running subtotals that are obviously not correctly ordered.
Question 2: Is this a bug, or evidence of a gap in my comprehension?
In the following call, I send a sequence that's explicitly ordered...
sequence f(for...$sorted-list[$i]/@e) = (0 8 12 14 15)
...and I receive the sequence I need. I can use this as a workaround.
Question 3: Why is it that I must explicitly [re-]sort the $sorted-list sequence as I pass it to util:f()?
这是我的输入 XML:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<contribution e="4"/>
<contribution e="1"/>
<contribution e="2"/>
<contribution e="8"/>
</root>
...还有我的 XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:util="http://www.method-r.com/util"
exclude-result-prefixes="xs util"
version="2.0">
<xsl:template match="/">
The following sequence is unsorted...
sequence $list = (<xsl:sequence select="$list"/>)
It is, as I expected, in document order.
The following sequence is explicitly sorted...
sequence $sorted-list = (<xsl:sequence select="$sorted-list"/>)
It is, as I expected, sorted in descending numerical @e order.
In the following output, I expect for the @e values from $list to be in document order,
and for the @e values from $sorted-list to be in descending numerical order...
value-of $list/@e = (<xsl:value-of select="$list/@e"/>)
value-of $sorted-list/@e = (<xsl:value-of select="$sorted-list/@e"/>)
But both are in document order.
Specifically, the $sorted-list/@e values are NOT listed in descending numerical @e order.
(Question 1: ...By the way, why can't I use 'xsl:sequence select="$list/@e"' here?)
Next, here's the real work that I'm interested in. It is a function that computes a running
subtotal of the elements passed in. Order is critical here. I expect for my sorted list to provide
@e values to the function in the order that I explicitly put them in the definition of $sorted-list.
So, in the following call, I send in the sequence that I think SHOULD have been sorted...
sequence f($sorted-list/@e) = (<xsl:sequence select="util:f(0, $sorted-list/@e)"/>)
But my result set is a list of running subtotals that are obviously not correctly ordered.
Question 2: Is this a bug, or evidence of a gap in my comprehension?
In the following call, I send a sequence that's explicitly ordered...
sequence f(for...$sorted-list[$i]/@e) = (<xsl:sequence select="util:f(0, for $i in 1 to count($sorted-list) return $sorted-list[$i]/@e)"/>)
...and I receive the sequence I need. I can use this as a workaround.
Question 3: Why is it that I must explicitly [re-]sort the $sorted-list sequence as I pass it to util:f()?
<xsl:text/>
</xsl:template>
<xsl:variable name="list" as="element()*" select="//contribution"/>
<xsl:variable name="sorted-list" as="element()*">
<xsl:perform-sort select="$list">
<xsl:sort select="number(@e)" order="descending"/>
</xsl:perform-sort>
</xsl:variable>
<xsl:function name="util:f" as="xs:double*">
<xsl:param name="sum0" as="xs:double"/>
<xsl:param name="list" as="xs:double*"/>
<xsl:sequence select="util:cumulative-list-2((), $list)"/>
</xsl:function>
<xsl:function name="util:cumulative-list-2" as="xs:double*">
<xsl:param name="list" as="xs:double*"/>
<xsl:param name="list-remainder" as="xs:double*"/>
<xsl:choose>
<xsl:when test="not(exists($list-remainder))">
<xsl:sequence select="$list"/>
</xsl:when>
<xsl:otherwise>
<xsl:sequence select="util:cumulative-list-2(
if (empty($list)) then
(0, $list-remainder[1])
else
($list, $list[last()] + $list-remainder[1]),
remove($list-remainder, 1)
)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:function>
</xsl:stylesheet>
最佳答案
通过执行 $sorted-list/@e 的步骤,/@e 将选择所有 @e 属性并按文档顺序排序因此,如果你有你的排序序列并想按序列的顺序输出 @e 属性,你不能使用 $sorted-list/@e,而是你必须使用 for $item in $sorted-list return $item/@e。
参见 http://www.w3.org/TR/xpath20/#id-path-expressions其中说:
Each operation E1/E2 is evaluated as follows: ... If every evaluation of E2 returns a (possibly empty) sequence of nodes, these sequences are combined, and duplicate nodes are eliminated based on node identity. The resulting node sequence is returned in document order.
如果您使用 Saxon 9.5 的商业版本,那么您应该也可以使用 http://www.w3.org/TR/xpath-30/#id-map-operator和 $sorted-list ! @e 保留顺序。
关于xml - 对排序序列的操作不遵守排序顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20435397/
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
我遵循了教程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
给定一个复杂的对象层次结构,幸运的是它不包含循环引用,我如何实现支持各种格式的序列化?我不是来讨论实际实现的。相反,我正在寻找可能会派上用场的设计模式提示。更准确地说:我正在使用Ruby,我想解析XML和JSON数据以构建复杂的对象层次结构。此外,应该可以将该层次结构序列化为JSON、XML和可能的HTML。我可以为此使用Builder模式吗?在任何提到的情况下,我都有某种结构化数据-无论是在内存中还是文本中-我想用它来构建其他东西。我认为将序列化逻辑与实际业务逻辑分开会很好,这样我以后就可以轻松支持多种XML格式。 最佳答案 我最
我有一个使用SeleniumWebdriver和Nokogiri的Ruby应用程序。我想选择一个类,然后对于那个类对应的每个div,我想根据div的内容执行一个Action。例如,我正在解析以下页面:https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=puppies这是一个搜索结果页面,我正在寻找描述中包含“Adoption”一词的第一个结果。因此机器人应该寻找带有className:"result"的div,对于每个检查它的.descriptiondiv是否包含单词“adoption
RSpec似乎按顺序匹配方法接收的消息。我不确定如何使以下代码工作:allow(a).toreceive(:f)expect(a).toreceive(:f).with(2)a.f(1)a.f(2)a.f(3)我问的原因是a.f的一些调用是由我的代码的上层控制的,所以我不能对这些方法调用添加期望。 最佳答案 RSpecspy是测试这种情况的一种方式。要监视一个方法,用allowstub,除了方法名称之外没有任何约束,调用该方法,然后expect确切的方法调用。例如:allow(a).toreceive(:f)a.f(2)a.f(1)
我正在我的Rails项目中安装Grape以构建RESTfulAPI。现在一些端点的操作需要身份验证,而另一些则不需要身份验证。例如,我有users端点,看起来像这样:moduleBackendmoduleV1classUsers现在如您所见,除了password/forget之外的所有操作都需要用户登录/验证。创建一个新的端点也没有意义,比如passwords并且只是删除password/forget从逻辑上讲,这个端点应该与用户资源。问题是Grapebefore过滤器没有像except,only这样的选项,我可以在其中说对某些操作应用过滤器。您通常如何干净利落地处理这种情况?
在我做的一些网络开发中,我有多个操作开始,比如对外部API的GET请求,我希望它们同时开始,因为一个不依赖另一个的结果。我希望事情能够在后台运行。我找到了concurrent-rubylibrary这似乎运作良好。通过将其混合到您创建的类中,该类的方法具有在后台线程上运行的异步版本。这导致我编写如下代码,其中FirstAsyncWorker和SecondAsyncWorker是我编写的类,我在其中混合了Concurrent::Async模块,并编写了一个名为“work”的方法来发送HTTP请求:defindexop1_result=FirstAsyncWorker.new.async.
假设我必须(小型到中型)阵列:tokens=["aaa","ccc","xxx","bbb","ccc","yyy","zzz"]template=["aaa","bbb","ccc"]如何确定tokens是否以相同的顺序包含template的所有条目?(请注意,在上面的示例中,应忽略第一个“ccc”,从而由于最后一个“ccc”而导致匹配。) 最佳答案 这适用于您的示例数据。tokens=["aaa","ccc","xxx","bbb","ccc","yyy","zzz"]template=["aaa","bbb","ccc"]po
我需要用任何语言编写一个算法,根据3个因素对数组进行排序。我以度假村为例(如Hipmunk)。假设我想去度假。我想要最便宜的地方、最好的评论和最多的景点。但是,显然我找不到在所有3个中都排名第一的方法。Example(assumingthereare20importantattractions):ResortA:$150/night...98/100infavorablereviews...18of20attractionsResortB:$99/night...85/100infavorablereviews...12of20attractionsResortC:$120/night
a=[3,4,7,8,3]b=[5,3,6,8,3]假设数组长度相同,是否有办法使用each或其他一些惯用方法从两个数组的每个元素中获取结果?不使用计数器?例如获取每个元素的乘积:[15,12,42,64,9](0..a.count-1).eachdo|i|太丑了...ruby1.9.3 最佳答案 使用Array.zip怎么样?:>>a=[3,4,7,8,3]=>[3,4,7,8,3]>>b=[5,3,6,8,3]=>[5,3,6,8,3]>>c=[]=>[]>>a.zip(b)do|i,j|c[[3,5],[4,3],[7,6],