【问题标题】:How to loop through the alphabet with XSLT?如何使用 XSLT 遍历字母表?
【发布时间】:2020-01-27 15:20:11
【问题描述】:

我需要在 XSLT 2.0 中创建一个动态 XSL-FO 表,看起来类似于:

+------+------+------+------+------+------+
|      |   1  |  2   |  3   |  4   |   5  |
+------+------+------+------+------+------+
|   A  |      |      |      |      |      |
+------+------+------+------+------+------+
|   B  |      |      |      |      |      |
+------+------+------+------+------+------+
|   C  |      |      |      |      |      |
+------+------+------+------+------+------+
|   D  |      |      |      |      |      |
+------+------+------+------+------+------+
|   E  |      |      |      |      |      |
+------+------+------+------+------+------+

两个终点(行、列)都是动态的,并使用源数据中的最后一个数字/字母指定。

<input>
    <end-stop-column>5</end-stop-column>
    <end-stop-row>E</end-stop-row>
</input>

我想到了for-each的方法(伪代码):

<fo:table>
    <fo:table-body>
        <fo:table-row>
            <fo:table-cell><fo:block></fo:block></fo:table-cell>
            <xsl:for-each select="1 to 5">
                <xsl:variable name="colid" select="current()"/>
                <fo:table-cell><fo:block><xsl:value-of select="$colid"/></fo:block></fo:table-cell>
            </xsl:for-each>
        </fo:table-row>
        <xsl:for-each select="A to E">
            <xsl:variable name="rowid" select="current()"/>
            <fo:table-row>
                <fo:table-cell><fo:block><xsl:value-of select="$rowid"/></fo:block></fo:table-cell>                    <xsl:for-each select="1 to 5">
                    <xsl:variable name="colid" select="current()"/>
                    <fo:table-cell>
                        <fo:block>
                            <xsl:text>Value for </xsl:text>
                            <xsl:value-of select="$rowid"/>
                            <xsl:text> </xsl:text>
                            <xsl:value-of select="$colid"/>
                        </fo:block>
                    </fo:table-cell>
                </xsl:for-each>
            </fo:table-row>
        </xsl:for-each>
    </fo:table-body>
</fo:table>

上述 XSLT 不起作用,因为 in 上下文中的 for-each 只能处理数字。

如何处理行中的字母?这些行可能不会在Z 停止,而是从ZAZB、...、ZZZZA、...重新开始。

【问题讨论】:

    标签: xslt xslt-2.0 xsl-fo


    【解决方案1】:

    使用从字母到数字的映射,如果 string-to-codepoints(end-stop-row) 足够 (https://www.w3.org/TR/xpath-functions/#func-string-to-codepoints)(将取决于您想到的字母表),请使用它。您在第二步中处理为字母的数字格式可以由xsl:numberformat-number 处理。

    【讨论】:

    • 它不能解决 Z, ZA... 问题,但我想我可以解决这个问题;例如通过拆分单个字符并添加(数学)它们。
    • xsl:numberformat-integer 将为您提供大于 26 的数字代码,但它们可能不是您想要的;它可能会去 AB、AC 等而不是 ZA、ZB。
    猜你喜欢
    • 2013-06-15
    • 2017-10-10
    • 2013-05-23
    • 2022-11-14
    • 2012-04-02
    • 2015-05-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多