简体   繁体   English

如何在XSLT 1.0中循环字符串标记?

[英]How can I loop on string tokens in XSLT 1.0?

I'd like to tokenize a string representing a date format like DD/MM/YYYY in many elements such as DD, /, MM, /, YYYY and loop on them in order to transform 我想在许多元素(例如DD,/,MM,/,YYYY)中标记表示日期格式(如DD / MM / YYYY)的字符串,并对其进行循环以进行转换

<tag date-format="DD/MM/YYYY" />

to

<container>
  <number:day number:style="long" />
  <number:text>/</number:text>
  <number:month number:style="long" />
  <number:text>/</number:text>
  <number:year number:style="long" />
</container>

Consequently, the tag 因此,标签

<tag date-format="MM-DD-YYYY" />

should be converted to 应该转换为

<container>
  <number:month number:style="long" />
  <number:text>-</number:text>
  <number:day number:style="long" />
  <number:text>-</number:text>
  <number:year number:style="long" />
</container>

I haven't the slightest idea how to do it in XSLT 1.0 whith no support of tokenizers nor regular expressions. 我完全不知道在XSLT 1.0中如何做到这一点,因为它既不支持分词器也不支持正则表达式。

Please, take into account I'm bound to XSLT 1.0 and cannot upgrade . 请考虑到我已绑定到XSLT 1.0并且无法升级

Try this as your starting point: 以此为起点:

<xsl:template match="tag[@date-format]">
    <container>
        <xsl:call-template name="analyze-date-format">
            <xsl:with-param name="date-format" select="@date-format" />
        </xsl:call-template>
    </container>
</xsl:template>

<xsl:template name="analyze-date-format">
    <xsl:param name="date-format"/>
    <xsl:variable name="separators" select="translate($date-format, 'YMD', '')" />
    <xsl:variable name="separator" select="substring($separators, 1, 1)" />
    <xsl:variable name="token">
        <xsl:choose>
            <xsl:when test="$separator">
                <xsl:value-of select="substring-before($date-format, $separator)" />
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$date-format" />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
    <xsl:choose>
        <xsl:when test="$token = 'YYYY'">
            <number:year number:style="long" />
        </xsl:when>
        <xsl:when test="$token = 'MM'">
            <number:month number:style="long" />
        </xsl:when>
        <xsl:when test="$token = 'DD'">
            <number:day number:style="long" />
        </xsl:when>
    </xsl:choose> 
    <xsl:if test="$separators">
        <number:text>
            <xsl:value-of select="$separator" />
        </number:text>
         <!-- recursive call -->
        <xsl:call-template name="analyze-date-format">
            <xsl:with-param name="date-format" select="substring-after($date-format, $separator)" />
        </xsl:call-template>
    </xsl:if>
</xsl:template>

This is assuming all tokens are in upper-case (and possibly some other presumptions too). 这是假设所有令牌都是大写的(可能还有其他一些假设)。 You'll probably want to add more tests for additional token types. 您可能需要为其他令牌类型添加更多测试。

Note that the number: prefix must be bound to a namespace! 注意, number:前缀必须绑定到名称空间!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM