2016-03-23 13 views
0

私のXMLをバックスラッシュをstirng翻訳:XSLT 1.0 - 二重のバックスラッシュに

<workorder> 
    <specifications> 
     <hpath>94 \ 72</hpath> 
     <classdesc></classdesc> 
    </specifications> 
</workorder> 

私のXSLT:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0"> 
<xsl:output method="text" encoding="UTF-8" /> 
     <xsl:template match="/"> 
      <xsl:apply-templates /> 
     </xsl:template> 

     <xsl:template match="workorder">   
     <xsl:apply-templates select="specifications" /> 
    </xsl:template> 

    <xsl:template match="specifications"> 
    <xsl:text>{ 
    "Hierarchy Path"="</xsl:text> 
     <xsl:value-of select="translate(//workorder/specifications/hpath,'\','\\')" /> 
     <xsl:text>" 
    }</xsl:text> 
    </xsl:template> 
    </xsl:stylesheet> 

期待出力は次のとおりです。

{ 
    "Hierarchy Path"="94 \\ 72" 
    } 

電流出力は次のようになります。

{ 
    "Hierarchy Path"="94 \ 72" 
    } 

問題:json形式を「現在の出力」として送信すると、有効なjson形式ではありません。 "期待される出力"として送信すると、有効なjson形式です。 助けてください。前もって感謝します。

答えて

2

XSLT 1.0では、再帰的なアプローチが必要です。
templateという名前のものを試してみることもできます。

<xsl:template name="jsonescape"> 
<xsl:param name="str" select="."/> 
    <xsl:choose> 
    <xsl:when test="contains($str, '\')"> 
     <xsl:value-of select="concat(substring-before($str, '\'), '\\')"/> 
     <xsl:call-template name="jsonescape"> 
     <xsl:with-param name="str" select="substring-after($str, '\')"/> 
     </xsl:call-template> 
    </xsl:when> 
    <xsl:otherwise> 
     <xsl:value-of select="$str"/> 
    </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 

そして、あなたの文字列(たとえば)でそれを呼び出します。

<xsl:call-template name="jsonescape"> 
    <xsl:with-param name="str" select="//workorder/specifications/hpath"/> 
</xsl:call-template> 
+0

非常にありがとうございます:) –

関連する問題