使用 xsltproc (XSLT 1.0)我试图从xsl-value @name属性中转义(“ - >到\”)内容。
XSL:
<xsl:template match="int:signature">
"name":"<xsl:value-of select="@name" mode="text"/>",
....
原始XML:
<signature name="My "case"" />
输出:
"name":"My "case"",
打破了生成的JSON
我尝试过使用 str:replace 但没有成功。 disable-output-escaping =“yes”也没有成功。
任何提示?
-
xsltproc -V
使用libxml 20706,libxslt 10126和libexslt 815
答案 0 :(得分:0)
这适用于XPath 2.0
"name":"<xsl:value-of select='fn:replace(@name, """", "\\""")' />"
据我所知,xsltproc不支持Xpath 2.0,但EXSLT扩展可能提供相同的功能
答案 1 :(得分:0)
如果你需要逃避这样的事情会有所帮助
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match ="signature">
<xsl:variable name="escape">
<xsl:call-template name="escape_quot">
<xsl:with-param name="replace" select="@name"/>
</xsl:call-template>
</xsl:variable>
"name":"<xsl:value-of select="$escape" />",
</xsl:template>
<xsl:template name="escape_quot">
<xsl:param name="replace"/>
<xsl:choose>
<xsl:when test="contains($replace,'"')">
<xsl:value-of select="substring-before($replace,'"')"/>
<!-- escape quot-->
<xsl:text>\"</xsl:text>
<xsl:call-template name="escape_quot">
<xsl:with-param name="replace" select="substring-after($replace,'"')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$replace"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
这将生成有意义的输出。
"name":"My \"case\"",
<强>更新强>
但是,将外向引号改为撇号是不够的。
使用:
"name":'<xsl:value-of select="@name" />',
得到:
"name":'My "case"',
(这应该是有效的JSON)