我一直在尋找一種方法去除我的XML內容的撇號('),因爲我的DBMS抱怨接收這些內容。使用XSLT消毒數據庫輸入
我需要
<name> Jim O'Connor</name>
成爲:
<name> Jim O''Connor</name>
通過查看示例中描述here,是應該有''
更換'
,我構建了下面的腳本:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes" />
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>
<xsl:template name="sqlApostrophe">
<xsl:param name="string" />
<xsl:variable name="apostrophe">'</xsl:variable>
<xsl:choose>
<xsl:when test="contains($string,$apostrophe)">
<xsl:value-of select="concat(substring-before($string,$apostrophe), $apostrophe,$apostrophe)"
disable-output-escaping="yes" />
<xsl:call-template name="sqlApostrophe">
<xsl:with-param name="string"
select="substring-after($string,$apostrophe)" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$string"
disable-output-escaping="yes" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="text()">
<xsl:call-template name="sqlApostrophe">
<xsl:with-param name="string" select="."/>
</xsl:call-template>
</xsl:template>
</xsl:stylesheet>
更新:它工作正常
感謝您的幫助
1)您使用什麼流程和語言來執行數據庫插入? 2)問題#1中的語言能否用於預處理單引號? – dacracot 2010-05-13 15:16:05
我使用一堆XPath表達式作爲參數調用SQL Server存儲過程 – azathoth 2010-05-13 15:31:02
您的存儲過程很麻煩。您應該在那裏解決問題,而不是在XSLT中。 – Alohci 2010-05-14 10:23:58