2016-05-15 39 views
1

xslt 1.0を使用して文字列の検索と置換を実装したいと考えています。 問題は、私は...例えば、私の入力XMLがからXSLT文字列の置換、複数の文字列

<process xmlns:client="http://xmlns.oracle.com/ErrorHandler" xmlns="http://xmlns.oracle.com/ErrorHandler"> 
    <client:result>The city name is London and Country name is England </client:result> 
</process> 

$ KEY1の$と$ KEY2 $でなければなりません

<process xmlns:client="http://xmlns.oracle.com/ErrorHandler" xmlns="http://xmlns.oracle.com/ErrorHandler"> 
    <client:result>The city name is $key1$ and Country name is $key2$ </client:result> 
</process> 

結果、以下のようである値が異なる複数の文字列を交換する必要があり、入力文字列をロンドンとイングランドに置き換える必要があります。 単一文字列を検索して置き換える例が多数見つかりましたが、複数の文字列を異なる値に置き換える方法がわかりません。 提案がありますか?事前

+1

は、ここでは一つの可能​​な方法を参照してください:http://stackoverflow.com/questions/33527077/change-html-dynamically-thru-xsl/33529970#33529970 –

答えて

1

おかげであなたはXSLT 2.0以上を使用することができれば、それははるかに容易になるだろう。入力として、以前の呼び出しの製品を使用して(あなたは、XSLT 1.0で立ち往生している場合はuse a recursive template to perform the replace、しかし

replace(replace(., '\$key1\$', 'London'), '\$key2\$', 'England') 

ことができ、および置換したい各トークンのためにそれを呼び出す:それはのような単純なものでした)次へ:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:client="http://xmlns.oracle.com/ErrorHandler" 
    version="1.0"> 

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

    <xsl:template match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="client:result"> 
     <xsl:variable name="orig" select="string(.)"/> 
     <xsl:variable name="key1"> 
      <xsl:call-template name="replace-string"> 
       <xsl:with-param name="text" select="$orig"/> 
       <xsl:with-param name="replace" select="'$key1$'"/> 
       <xsl:with-param name="with" select="'London'"/> 
      </xsl:call-template> 
     </xsl:variable> 
     <xsl:variable name="key2"> 
      <xsl:call-template name="replace-string"> 
       <xsl:with-param name="text" select="$key1"/> 
       <xsl:with-param name="replace" select="'$key2$'"/> 
       <xsl:with-param name="with" select="'England'"/> 
      </xsl:call-template> 
     </xsl:variable> 

     <xsl:copy> 
      <xsl:value-of select="$key2"/> 
     </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 
+0

はどうもありがとうございました –