2012-06-23 16 views

risposta

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

in funzione:

<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

Sto usando xslt1.0. le tokenize e le funzioni di rimozione non sono supportate – Suresh

0

bene XSLT 1.0 molto più impegnativo:

ho messo due modelli

2.after-delim (uscite valore successivo e si chiama se un altro delim) 3.before-last-delim (inizia il processo)

<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

Questo è essenzialmente la stessa soluzione, ma al fine di preservare le delimitatori intermedi nel risultato, i seguenti tre righe deve aggiungere:

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

Tutta trasformazione diventa ora:

<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> 

Quando questa trasformazione viene applicato su qualsiasi documento XML (non utilizzato), il voluto, risultato corretto viene prodotto:

ABC_12345 
Problemi correlati