Xpath 将节点转换为 mod 的数字

发布于 2024-12-23 02:25:46 字数 419 浏览 3 评论 0原文

我有包含数字的节点,我想将其转换为数字并使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

诺曦 2024-12-30 02:25:46

number(num) mod 3 应该可以。以下示例文件按预期输出 1 2 0

XML

(保存为 input.xml

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="mod_test.xsl"?>
<items>
    <item>
        <num>1</num>
    </item>
    <item>
        <num>2</num>
    </item>
    <item>
        <num>3</num>
    </item>
</items>

XSL

(保存为 mod_text.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="xsl">
<xsl:output omit-xml-declaration="yes" indent="yes"/>

<xsl:template match="//item">
    <xsl:value-of select="number(num) mod 3"/>
</xsl:template>
</xsl:stylesheet>

注意:选择中的 num mod 3 也有效。

作为参考,这里是文档中的相关部分

number(num) mod 3 should work. The following example files output 1 2 0 as expected.

XML

(saved as input.xml)

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="mod_test.xsl"?>
<items>
    <item>
        <num>1</num>
    </item>
    <item>
        <num>2</num>
    </item>
    <item>
        <num>3</num>
    </item>
</items>

XSL

(saved as mod_text.xsl)

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="xsl">
<xsl:output omit-xml-declaration="yes" indent="yes"/>

<xsl:template match="//item">
    <xsl:value-of select="number(num) mod 3"/>
</xsl:template>
</xsl:stylesheet>

Note: just num mod 3 in the select also works.

For reference, here is the relevant section in the documentation.

乖不如嘢 2024-12-30 02:25:46

我已经尝试过:

num mod 3 -- 返回 NaN 
number(num) mod 3 -- 返回 NaN 
number(string(num)) -- 返回 NaN 

知道这是否可以做到吗?

由于没有提供完整的 XML 文档,以下是我的两个猜测

  1. 相对表达式的上下文节点没有 num 个子节点。解决方案是确保上下文节点是正确的,或者使用绝对 XPath 表达式。

  2. 未显示的 XML 文档位于默认命名空间中。在这种情况下,解决方案是“注册一个名称空间”(将字符串前缀与默认名称空间相关联,例如 "x"),然后在您的表达式中替换 num 与 x:num

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?

As no complete XML document is provided, here are my two guesses:

  1. 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).

  2. 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 with x:num.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文