我是XSLT 1.0的新手。经过多次尝试使用substring-before和substring-after后,我被困在了下面。
我的输入字符串是
txt=3000000-User from United Kingdom3000006-Do not know where user is from3000004-User only provide address
我需要的输出如下:
<Line>3000000</Line>
<Line>3000004</Line>
<Line>3000006</Line>
我使用的代码无法知道自动循环字符串中的下一个数字
<Line><xsl:value-of select="substring(normalize-space(translate(translate(substring-after(txt, '-'), $uppercase, $smallcase), $smallcase, ' ')), 0, 8)"/></Line>
答案 0 :(得分:1)
您需要在此处调用递归模板。假设任何两个数字之间总是至少有一个连字符,请尝试:
...
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="normalize-space(translate(translate(txt, translate(txt, '-0123456789', ''), ''), '-', ' '))"/>
</xsl:call-template>
...
<xsl:template name="tokenize">
<xsl:param name="text"/>
<xsl:param name="delimiter" select="' '"/>
<xsl:choose>
<xsl:when test="contains($text, $delimiter)">
<line>
<xsl:value-of select="substring-before($text, $delimiter)"/>
</line>
<!-- recursive call -->
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="substring-after($text, $delimiter)"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<line>
<xsl:value-of select="$text"/>
</line>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
或者,如果每个数字正好是7位数,后跟连字符和,则文本中没有其他连字符,您可以使这更简单:
...
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="txt"/>
</xsl:call-template>
...
<xsl:template name="tokenize">
<xsl:param name="text"/>
<xsl:if test="contains($text, '-')">
<xsl:param name="token" select="substring-before($text, '-')"/>
<line>
<xsl:value-of select="substring($token, string-length($token) - 6)"/>
</line>
<!-- recursive call -->
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="substring-after($text, '-')"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
答案 1 :(得分:0)
试试这个
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="tag">
<xsl:variable name="NumberWithDots" select="translate(., 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ: ', '')"/>
<Line>
<xsl:choose>
<xsl:when test="contains($NumberWithDots, '.')">
<xsl:value-of select="substring-before($NumberWithDots, '.')"/>
<xsl:text>.</xsl:text>
<xsl:value-of select="translate(substring-after($NumberWithDots, '.'), '.', '')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="number($NumberWithDots)"/>
</xsl:otherwise>
</xsl:choose>
</Line>
</xsl:template>
</xsl:stylesheet>