2012-06-23 19 views

Respuesta

0
<xsl:variable name="s">string_to_cut_no_matter_how_many_underscores</xsl:variable> 
<xsl:variable name="s-tokenized" select="tokenize($s, '_')"/> 
<xsl:variable name="s_final" select="string-join(remove($s-tokenized, count($s-tokenized)),'_')"/> 

como una función:

<xsl:function name="mynamespace:before-last-delimeter"> 
<xsl:param name="s" /> 
<xsl:param name="d" /> 

<xsl:variable name="s-tokenized" select="tokenize($s, $d)"/> 

<xsl:value-of select="string-join(remove($s-tokenized, count($s-tokenized)),$d)"/> 


</xsl:function> 


<xsl:variable name="output" select="mynamespace:before-last-delimeter('I-only-want-before-the-last-dash','-')"/> 
+0

Estoy usando xslt1.0. tokenize y eliminar funciones no son compatibles – Suresh

0

bien XSLT 1.0 mucho más difícil:

Me llevó dos plantillas

2.after-delim (salidas siguiente valor y llama a sí misma si otro delim) 3.Antes-última-DELIM (aperturas el proceso,)

<xsl:template name="after-delim"> 
    <xsl:param name="s"/> 
    <xsl:param name="d"/> 
    <xsl:choose> 
     <xsl:when test="contains($s,$d)"> 
      <xsl:value-of select="concat($d,substring-before($s,$d))"/> 
      <xsl:call-template name="after-delim"> 
      <xsl:with-param name="s" select="substring-after($s,$d)"/> 
      <xsl:with-param name="d" select="$d"/> 
      </xsl:call-template> 
     </xsl:when> 
     <xsl:otherwise> 
     </xsl:otherwise> 
    </xsl:choose> 
    </xsl:template> 



    <xsl:template name="before-last-delim"> 
    <xsl:param name="s"/> 
    <xsl:param name="d"/> 
    <xsl:choose> 
     <xsl:when test="contains($s,$d)"> 
      <xsl:value-of select="substring-before($s,$d)"/> 
      <xsl:call-template name="after-delim"> 
      <xsl:with-param name="s" select="substring-after($s,$d)"/> 
      <xsl:with-param name="d" select="$d"/> 
      </xsl:call-template> 
     </xsl:when> 
     <xsl:otherwise> 
     <xsl:value-of select="$s"/> 
     </xsl:otherwise> 
    </xsl:choose> 

    </xsl:template> 

<xsl:call-template name="before-last-delim"> 


    <xsl:with-param name="s" select="'string_to_cut_with_delim'"/> 
    <xsl:with-param name="d" select="'_'"/> 

</xsl:call-template> 
3

Esta es esencialmente la misma solución, pero a fin de preservar los delimitadores intermedios en el resultado, las tres líneas siguientes se debe añadir:

<xsl:if test="contains(substring-after($pText, $pDelim), $pDelim)"> 
    <xsl:value-of select="$pDelim"/> 
</xsl:if> 

El conjunto de transformación se convierte ahora en:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="text"/> 

     <xsl:variable name="s" select="'ABC_12345_Q-10'"/> 
     <xsl:template match="/"> 
     <xsl:call-template name="stripLast"> 
     <xsl:with-param name="pText" select="$s"/> 
     </xsl:call-template> 
     </xsl:template> 

     <xsl:template name="stripLast"> 
     <xsl:param name="pText"/> 
     <xsl:param name="pDelim" select="'_'"/> 

     <xsl:if test="contains($pText, $pDelim)"> 
      <xsl:value-of select="substring-before($pText, $pDelim)"/> 
      <xsl:if test="contains(substring-after($pText, $pDelim), $pDelim)"> 
      <xsl:value-of select="$pDelim"/> 
      </xsl:if> 
      <xsl:call-template name="stripLast"> 
      <xsl:with-param name="pText" select= 
       "substring-after($pText, $pDelim)"/> 
      <xsl:with-param name="pDelim" select="$pDelim"/> 
      </xsl:call-template> 
     </xsl:if> 
     </xsl:template> 
</xsl:stylesheet> 

Cuando se aplica esta transformación en cualquier documento XML (no se utiliza), el, resultado correcto querido se produce:

ABC_12345 
Cuestiones relacionadas