ฉันมีสตริงดังนี้และพยายามแปลงเป็นรูปแบบ json:
ทดสอบ "out" และ \new
ผลลัพธ์ที่คาดหวังคือ Test \"out\" และ \new
ฉันลองโดยเรียกเทมเพลตสำหรับ Escapequote - ทำงานได้ดีสำหรับเครื่องหมายคำพูด Escape:
<xsl:template name="escapeQuote">
<xsl:param name="pText" select="concat(normalize-space(.), '')" />
<xsl:if test="string-length($pText) >0">
<xsl:value-of select="substring-before(concat($pText, '"'), '"')" />
<xsl:if test="contains($pText, '"')">
<xsl:text>\"</xsl:text>
<xsl:call-template name="escapeQuote">
<xsl:with-param name="pText" select="substring-after($pText, '"')" />
</xsl:call-template>
</xsl:if>
</xsl:if>
</xsl:template>
เทมเพลตสำหรับแบ็กสแลช Escape - ใช้งานได้กับแบ็กสแลชเท่านั้น:
<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>
คำถามของฉันจะเรียกทั้งสองเทมเพลตหรือรวมได้อย่างไร โปรดช่วยฉันด้วย