7

私の質問は最後の出現を見つけることに似ています

ただし、区切り文字が最後に出現する直前に、文字列全体を出力する必要があります。したがって、例の出力は次のようになりますABC_12345

XSLT 1.0 である必要があります

4

4 に答える 4

8

substring-before-last別の質問のために実装したテンプレートを見てください。

XSLT 文字列の最後の文字を削除する

それはまさにあなたが必要としているもののようです。

于 2012-06-23T06:24:00.687 に答える
3

これは基本的に同じ解決策ですが、結果の中間区切り文字を保持するには、次の 3 行を追加する必要があります

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

全体の変換は次のようになります。

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

この変換が任意の XML ドキュメント (使用されていない) に適用されると、必要な正しい結果が生成されます。

ABC_12345
于 2012-06-23T14:57:02.160 に答える
0

わかりました XSLT 1.0 はもっと挑戦的です:

2 つのテンプレートが必要でした

2.after-delim (次の値を出力し、別の delim の場合は自分自身を呼び出します) 3.before-last-delim (プロセスを開始します)

    <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>
于 2012-06-23T06:26:38.760 に答える
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)),'_')"/>

関数として:

<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','-')"/>
于 2012-06-23T04:50:49.113 に答える