用递归进行XSLT多字符串替换(XSLT multiple string replacement with recursion)
我一直在试图用递归执行多个(不同的)字符串替换,并且遇到了障碍。 我已经成功地获得了第一个替代品,但随后的替代品从未开火。 我知道这与递归有关,以及with-param字符串如何传递回调用模板。 我看到我的错误,为什么下一个xsl:当永远不会触发,但我似乎无法弄清楚如何将完整的修改后的字符串从第一个xsl:何时传递到第二个xsl:when。 任何帮助是极大的赞赏。
<xsl:template name="replace"> <xsl:param name="string" select="." /> <xsl:choose> <xsl:when test="contains($string, ' ')"> <xsl:value-of select="substring-before($string, ' ')" /> <br/> <xsl:call-template name="replace"> <xsl:with-param name="string" select="substring-after($string, ' ')"/> </xsl:call-template> </xsl:when> <xsl:when test="contains($string, 'TXT')"> <xsl:value-of select="substring-before($string, ' TXT')" /> <xsl:call-template name="replace"> <xsl:with-param name="string" select="substring-after($string, ' ')" /> </xsl:call-template> </xsl:when> <xsl:otherwise> <xsl:value-of select="$string"/> </xsl:otherwise> </xsl:choose> </xsl:template>
I have been attempting to perform multiple (different) string replacement with recursion and I have hit a roadblock. I have sucessfully gotten the first replacement to work, but the subsequent replacements never fire. I know this has to do with the recursion and how the with-param string is passed back into the call-template. I see my error and why the next xsl:when never fires, but I just cant seem to figure out exactly how to pass the complete modified string from the first xsl:when to the second xsl:when. Any help is greatly appreciated.
<xsl:template name="replace"> <xsl:param name="string" select="." /> <xsl:choose> <xsl:when test="contains($string, ' ')"> <xsl:value-of select="substring-before($string, ' ')" /> <br/> <xsl:call-template name="replace"> <xsl:with-param name="string" select="substring-after($string, ' ')"/> </xsl:call-template> </xsl:when> <xsl:when test="contains($string, 'TXT')"> <xsl:value-of select="substring-before($string, ' TXT')" /> <xsl:call-template name="replace"> <xsl:with-param name="string" select="substring-after($string, ' ')" /> </xsl:call-template> </xsl:when> <xsl:otherwise> <xsl:value-of select="$string"/> </xsl:otherwise> </xsl:choose> </xsl:template>
原文:https://stackoverflow.com/questions/5213644
最满意答案
不,矢量的容量不会因
reserve
或resize
而减少。如果
new_cap
是[不大于capacity
] ...没有迭代器或引用无效。调整大小到更小时,向量容量永远不会减少,因为这会使所有迭代器无效,而不是只有那些由等效的pop_back()调用序列无效的迭代器。
No, the vector's capacity will not be reduced by
reserve
orresize
.If
new_cap
is [not greater thancapacity
] ... no iterators or references are invalidated.Vector capacity is never reduced when resizing to smaller size because that would invalidate all iterators, rather than only the ones that would be invalidated by the equivalent sequence of pop_back() calls.
相关问答
更多-
如何缩小std :: vector?(How to downsize std::vector?)[2022-04-10]
有效的STL,Scott Meyers,项目17:使用swap技巧来削减过剩的产能。 vector(persons).swap(persons); 之后, persons “缩水到适合”。 这依赖于vector的拷贝构造函数只为需要复制的元素分配内存。 Effective STL, by Scott Meyers, Item 17: Use the swap trick to trim excess capacity. vector (persons).swap(person ... -
Javadoc,Javadoc,Javadoc。 这是你看起来的第一个地方。 在HashMap它说: 通常,默认加载因子(.75)在时间和空间成本之间提供了良好的折衷。 较高的值会减少空间开销,但会增加查找成本(反映在大部分HashMap类的操作中,包括get和put)。 在设置初始容量时,应考虑映射中的条目数量及其负载因子,以尽量减少重新运行操作的次数。 如果初始容量大于最大入口数除以负载因子,则不会发生重新刷新操作。 就散列图的理论而言 - 如果你的地图已满,那么你正在做一些非常非常错误的事情。 那时你 ...
-
我总是被教导说,没有保证标准的方法来降低容量。 所有的方法都已经(现在仍然是)实现定义的。 §23.2.1 \ 8说: 对于非array的标准容器类型的容器a和b ,表达式a.swap(b)应交换a和b的值,而不对每个容器元素调用任何移动,复制或交换操作。 这保证了矢量的内部指针必须交换。 但是,我找不到任何能够保证新建矢量能力的东西。 §21.4.2 \ 1说basic_string默认构造函数的后置条件之一是capacity()返回一个未指定的值。 §21.4.2 \ 3说basic_string拷贝构 ...
-
不,矢量的容量不会因reserve或resize而减少。 std::vector::reserve : 如果new_cap是[不大于capacity ] ...没有迭代器或引用无效。 std::vector::resize 调整大小到更小时,向量容量永远不会减少,因为这会使所有迭代器无效,而不是只有那些由等效的pop_back()调用序列无效的迭代器。 标准的相关部分 No, the vector's capacity will not be reduced by reserve or resize. st ...
-
也许你对std::vector::resize功能感到困惑。 它缩小或增大对象的大小,使它具有该函数的大小。 通过使用 v.resize (100); 你问v要调整到100个元素。 之后,电话 v.insert (v.begin(), 2); 对象的大小将为101.如果当前容量小于101,则需要增加容量。 也许你打算使用std::vector::reserve 。 如果有必要,它可以在不增加尺寸的情况下增加物体的容量。 Perhaps you are confused about what std::v ...
-
在某些实现中, resize几乎肯定会首先调用reserve 。 最近实现了一个std::vector的变体,下面是std::vector::reserve一个剥离和注释版本...(这些注释是OP的理解) ......实际上,大多数STL实现会稍微比这更复杂(用于调试目的); 但它的概念基本相同。 template
void Vector ::reserve(SizeType sz){ //if only the new size is greater than ... -
std :: vector preallocation(大小n,容量n + 2)(std::vector preallocation (size n, capacity n + 2))[2022-03-09]
您可以使用v.reserve(n + 2)更改vector的容量而不更改其大小。 查看文档以更好地了解正在发生的事情。 You can use v.reserve(n + 2) to change the vector's capacity without altering its size. Take a look at the documentation to better understand what is going on. -
减少矢量的容量涉及到 分配新存储 复制现有元素 解除分配旧存储空间 这些都不是免费的。 真正缩小向量会产生相关的成本,并且用户默认情况下应该付费。 Reducing the capacity of a vector involves allocating new storage copying over the existing elements deallocating old storage None of this is for free. Really shrinking a vector has ...
-
在C ++中,您可以选择通过值 , 引用或指针进行传递,具体取决于具体情况。 您将按向量传递矢量到函数中。 正在制作载体的副本。 向量复制构造函数使新向量的容量与从中复制的向量的大小相同。 如果您通过引用或指针传递向量,而不进行复制,则输出原始向量的大小/容量: void sizeCheck(std::vector
&test) { std::cout << "Size: " << test.size() << std::endl; std::cout << "Capacity ... -
这取决于实现,所以在切换操作系统,编译器等时不要指望相同的模式。 最常见的增长模式是1.5 * previous_capacity和2 * previous_capacity 。 在你的例子中,它似乎是前者。 有关为何选择该因子的可能解释,请参阅https://stackoverflow.com/a/1100426/784668 。 关键是它显然允许重用以前用于存储阵列的空闲内存块。 It depends on the implementation, so don't expect the same pat ...