Xpath 将节点转换为 mod 的数字
我有包含数字的节点,我想将其转换为数字并使用 mod。例如:
<item>
<num>1</num>
</item>
<item>
<num>2</num>
</item>
<item>
<num>3</num>
</item>
我尝试过:
num mod 3 -- returns NaN
number(num) mod 3 -- returns NaN
number(string(num)) -- returns NaN
知道这是否可以做到吗?即使有办法转换为 ASCII,我也会接受,
提前致谢!
I have nodes that contain numbers that I would like to cast to a number and use mod. For example:
<item>
<num>1</num>
</item>
<item>
<num>2</num>
</item>
<item>
<num>3</num>
</item>
I've tried:
num mod 3 -- returns NaN
number(num) mod 3 -- returns NaN
number(string(num)) -- returns NaN
Any idea if this can be done? Even if there was a way to convert to ASCII, I would take it
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
number(num) mod 3
应该可以。以下示例文件按预期输出1 2 0
。XML
(保存为
input.xml
)XSL
(保存为
mod_text.xsl
)注意:选择中的
num mod 3
也有效。作为参考,这里是文档中的相关部分。
number(num) mod 3
should work. The following example files output1 2 0
as expected.XML
(saved as
input.xml
)XSL
(saved as
mod_text.xsl
)Note: just
num mod 3
in the select also works.For reference, here is the relevant section in the documentation.
由于没有提供完整的 XML 文档,以下是我的两个猜测:
相对表达式的上下文节点没有
num
个子节点。解决方案是确保上下文节点是正确的,或者使用绝对 XPath 表达式。未显示的 XML 文档位于默认命名空间中。在这种情况下,解决方案是“注册一个名称空间”(将字符串前缀与默认名称空间相关联,例如
"x"
),然后在您的表达式中替换num 与
x:num
。As no complete XML document is provided, here are my two guesses:
The context node for the relative expressions has no
num
children. The solution is to assure that the context node is the correct one, or to use absolute XPath expression(s).The not-shown XML document is in a default namespace. In this case the solution is to "register a namespace" (associate a string-prefix to the default namespace, say
"x"
) and then replace in your expressio(s)num
withx:num
.