首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在XSLT1.0中循环字符串标记?

如何在XSLT1.0中循环字符串标记?
EN

Stack Overflow用户
提问于 2016-05-03 09:04:40
回答 1查看 183关注 0票数 0

我想在许多元素(如DD、/、MM、/、YYYY和循环)中标记一个字符串,表示DD/MM/YYYY等日期格式,以便进行转换。

代码语言:javascript
复制
<tag date-format="DD/MM/YYYY" />

代码语言:javascript
复制
<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>

因此,标签

代码语言:javascript
复制
<tag date-format="MM-DD-YYYY" />

应该转换为

代码语言:javascript
复制
<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>

我一点也不知道在XSLT1.0中如何做到这一点,因为它既不支持标记器,也不支持正则表达式。

请考虑到,我绑定到XSLT1.0,不能升级

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-03 09:53:05

试着把这个作为你的出发点:

代码语言:javascript
复制
<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>

这是假设所有的标记都是大写的(可能还有一些其他的假设)。您可能希望为其他令牌类型添加更多的测试。

注意,number:前缀必须绑定到命名空间!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36999932

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档