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

Java XSL替换字符串值中的两种类型的字符,java,regex,xml,xslt,Java,Regex,Xml,Xslt,假设我们有两种情况,XML可能包含带有字符/或-的生日。示例:1966-02-02或1977/03/04 我想删除这些字符,使值分别变为19660202和19770304 现在,我当前的xsl如下所示: <xsl:if test="$person/birthDate != '' "> <xsl:attribute name="birthday"> <xsl:variable

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

我想删除这些字符,使值分别变为
19660202
19770304

现在,我当前的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>


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

如果您只想删除
/
-
,则不需要使用正则表达式,只需使用

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

或者,为了避免在两个地方执行此操作,您可以在variable语句中使用它,如下所示:

<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, '/|-', '')"/>


使用。

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

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

或者,为了避免在两个地方执行此操作,您可以在variable语句中使用它,如下所示:

<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, '/|-', '')"/>


使用。

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

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

或者,为了避免在两个地方执行此操作,您可以在variable语句中使用它,如下所示:

<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, '/|-', '')"/>


使用。

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

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

或者,为了避免在两个地方执行此操作,您可以在variable语句中使用它,如下所示:

<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, '/|-', '')"/>


使用。

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

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


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

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

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


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

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

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


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

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

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


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

请。。。花几秒钟的时间以可读的方式布局代码,为什么所有缩进都在右边?请。。。花几秒钟的时间以可读的方式布局代码,为什么所有缩进都在右边?请。。。花几秒钟的时间以可读的方式布局代码,为什么所有缩进都在右边?请。。。花几秒钟的时间以可读的方式布局代码,为什么所有缩进都在右边?只有当OP使用XSLT 2.0或3.0时,
replace
函数才能工作,
translate
解决方案可以与XSLT的任何版本一起工作。只有当OP使用XSLT 2.0或3.0时,
replace
函数才能工作,
translate
解决方案将与XSLT的任何版本一起工作。
replace
函数仅在OP使用XSLT 2.0或3.0时才起作用,
translate
解决方案将与XSLT的任何版本一起工作。
replace
函数仅在OP使用XSLT 2.0或3.0时起作用,
translate
解决方案将适用于任何XSLT版本。