XSL在字符串值中替换两种类型的字符

时间:2015-08-25 21:39:52

标签: java regex xml xslt

因此,假设我们有两种情况,XML可能包含带有字符/-的生日。示例:1966-02-021977/03/04

我想删除这些字符,因此值分别为1966020219770304

现在,我当前的xsl看起来像这样:

<xsl:if test="$person/birthDate != '' ">
  <xsl:attribute name="birthday">                                        
    <xsl:variable name="dob" select="$person/birthDate"/>
    <xsl:choose>
      <xsl:when test="string-length($dob)>10">
        <xsl:value-of select="substring($dob,1,10)"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$dob"/>
      </xsl:otherwise>   
    </xsl:choose>
  </xsl:attribute>
</xsl:if>

如何在这两种情况下删除这些字符?

2 个答案:

答案 0 :(得分:2)

如果您只想删除/-,那么您不需要使用正则表达式,只需使用translate function即可删除它们

<xsl:value-of select="translate($dob, '/-', '')"/>

或者,为了在两个地方保存,你可以在变量语句中使用它,如下所示:

<xsl:variable name="dob" select="translate($person/birthDate, '/-', '')"/>
<xsl:choose>
    <xsl:when test="string-length($dob)>8"><xsl:value-of select="substring($dob,1,8)"/></xsl:when>
    <xsl:otherwise><xsl:value-of select="$dob"/></xsl:otherwise>
</xsl:choose>

如果你确实想要使用正则表达式,你可以这样编码:

<xsl:variable name="dob" select="replace($person/birthDate, '/|-', '')"/>

使用replace function

答案 1 :(得分:1)

或者您可以使用双翻译功能

<xsl:value-of select="translate($dob, translate($dob, '0123456789', ''), '')"/>

这将只提取字符串中的数字

相关问题