XSL:for 循环中的标记化问题

发布于 2024-12-19 01:08:39 字数 957 浏览 1 评论 0原文

我使用以下样式表在 Excel 工作簿中显示包含数据的表格。我无法获得所需的结果,而是显示的结果与下面给出的不同。

请提出建议?

使用的样式表:

<xsl:stylesheet>
<xsl:template match="/">
    <xsl:variable name="test1" select="str:tokenize('1$,$2$,$3$,$4$,$5','$,$')" />
    <xsl:variable name="test2" select="str:tokenize('a$,$b$,$c$,$d$,$e','$,$')" />
    <xsl:for-each select="str:split('1a$,$2b$,$3c$,$4d$,$5e','$,$')>
        <row>
            <cell Index="1">
                <xsl:value-of select="$test1[position()]" />
            </cell>
            <cell Index="2">
                <xsl:value-of select="$test2[position()]" />
            </cell>
        </row>
    </xsl:for-each>
</xsl:template>

预期结果:

1 a

2 b

3 c

4 d

5 e

结果显示为

a b

c d

e

看起来它正在显示最新的 tokenize 值。 如何获得受人尊重的价值观。

I am using the following stylesheet for displaying a table in the excel workbook with the data. I am not able to get the desired result instead it is displaying as differently as given below.

Suggestions Pls?

The stylesheet used:

<xsl:stylesheet>
<xsl:template match="/">
    <xsl:variable name="test1" select="str:tokenize('1$,$2$,$3$,$4$,$5','$,

Expected Result:

1 a

2 b

3 c

4 d

5 e

Where as the result displayed as

a b

c d

e

It seems like the it is displaying the latest tokenize values.
How to get respected values.

)" /> <xsl:variable name="test2" select="str:tokenize('a$,$b$,$c$,$d$,$e','$,

Expected Result:

1 a

2 b

3 c

4 d

5 e

Where as the result displayed as

a b

c d

e

It seems like the it is displaying the latest tokenize values.
How to get respected values.

)" /> <xsl:for-each select="str:split('1a$,$2b$,$3c$,$4d$,$5e','$,

Expected Result:

1 a

2 b

3 c

4 d

5 e

Where as the result displayed as

a b

c d

e

It seems like the it is displaying the latest tokenize values.
How to get respected values.

)> <row> <cell Index="1"> <xsl:value-of select="$test1[position()]" /> </cell> <cell Index="2"> <xsl:value-of select="$test2[position()]" /> </cell> </row> </xsl:for-each> </xsl:template>

Expected Result:

1 a

2 b

3 c

4 d

5 e

Where as the result displayed as

a b

c d

e

It seems like the it is displaying the latest tokenize values.
How to get respected values.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

怼怹恏 2024-12-26 01:08:39

好问题,+1。

在我看来,而不是

<xsl:value-of select="$test1[position()]" />

这必须是

<xsl:value-of select="$test1[position() = current()]" />

第二个观察结果完全相同

解释

任何表达式都

 $var[position()]

等价于:

$var

因为 position() 只能有值 >= 1 和 [position()]< /code> 表示 布尔值 position() ,任何非负数的布尔值根据定义都是true()

如果我们想在 XPath 1.0 中选择节点集 $var 中的第 $k 个节点,该节点是弱类型的并且不知道 $k 包含一个整数,我们有写:

$var[position() = $k]

这是一个完整的、相应的 XSLT 2.0 解决方案

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>

    <xsl:template match="/">
        <xsl:variable name="test1" select=
        "tokenize('1$,$2$,$3$,$4$,$5','\$,\

当应用于任何 XML 文档(忽略)时,会产生所需的正确结果

<row xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <cell Index="1">1 2 3 4 5</cell>
   <cell Index="2">a b c d e</cell>
</row>
<row xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <cell Index="1">1 2 3 4 5</cell>
   <cell Index="2">a b c d e</cell>
</row>
<row xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <cell Index="1">1 2 3 4 5</cell>
   <cell Index="2">a b c d e</cell>
</row>
<row xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <cell Index="1">1 2 3 4 5</cell>
   <cell Index="2">a b c d e</cell>
</row>
<row xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <cell Index="1">1 2 3 4 5</cell>
   <cell Index="2">a b c d e</cell>
</row>
)" /> <xsl:variable name="test2" select= "tokenize('a$,$b$,$c$,$d$,$e','\$,\

当应用于任何 XML 文档(忽略)时,会产生所需的正确结果


)" />

        <xsl:for-each select="tokenize('1a$,$2b$,$3c$,$4d$,$5e','\$,\

当应用于任何 XML 文档(忽略)时,会产生所需的正确结果


)">
            <row>
                <cell Index="1">
                    <xsl:value-of select="$test1[position()]" />
                </cell>
                <cell Index="2">
                    <xsl:value-of select="$test2[position()]" />
                </cell>
            </row>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

当应用于任何 XML 文档(忽略)时,会产生所需的正确结果

Good question, +1.

It seems to me that instead of:

<xsl:value-of select="$test1[position()]" />

this must be:

<xsl:value-of select="$test1[position() = current()]" />

Exactly the same observation holds for the second <xsl:value-of>

Explanation:

Any expression

 $var[position()]

is equivalent to:

$var

because position() can only have values >= 1 and [position()] means the boolean value of position() , and the boolean value of any non-negative number by definition is true().

If we want to select the $k-th node in the node-set $var, in XPath 1.0, which is weakly-typed and it isn't known that $k holds an integer, we have to write:

$var[position() = $k]

Here is a complete, corresponding XSLT 2.0 solution:

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>

    <xsl:template match="/">
        <xsl:variable name="test1" select=
        "tokenize('1$,$2$,$3$,$4$,$5','\$,\

when applied on any XML document (ignored), the wanted, correct result is produced:

<row xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <cell Index="1">1 2 3 4 5</cell>
   <cell Index="2">a b c d e</cell>
</row>
<row xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <cell Index="1">1 2 3 4 5</cell>
   <cell Index="2">a b c d e</cell>
</row>
<row xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <cell Index="1">1 2 3 4 5</cell>
   <cell Index="2">a b c d e</cell>
</row>
<row xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <cell Index="1">1 2 3 4 5</cell>
   <cell Index="2">a b c d e</cell>
</row>
<row xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <cell Index="1">1 2 3 4 5</cell>
   <cell Index="2">a b c d e</cell>
</row>
)" /> <xsl:variable name="test2" select= "tokenize('a$,$b$,$c$,$d$,$e','\$,\

when applied on any XML document (ignored), the wanted, correct result is produced:


)" />

        <xsl:for-each select="tokenize('1a$,$2b$,$3c$,$4d$,$5e','\$,\

when applied on any XML document (ignored), the wanted, correct result is produced:


)">
            <row>
                <cell Index="1">
                    <xsl:value-of select="$test1[position()]" />
                </cell>
                <cell Index="2">
                    <xsl:value-of select="$test2[position()]" />
                </cell>
            </row>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

when applied on any XML document (ignored), the wanted, correct result is produced:

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