2010-05-13 137 views
0

我一直在尋找一種方法去除我的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> 

更新:它工作正常

感謝您的幫助

+0

1)您使用什麼流程和語言來執行數據庫插入? 2)問題#1中的語言能否用於預處理單引號? – dacracot 2010-05-13 15:16:05

+0

我使用一堆XPath表達式作爲參數調用SQL Server存儲過程 – azathoth 2010-05-13 15:31:02

+1

您的存儲過程很麻煩。您應該在那裏解決問題,而不是在XSLT中。 – Alohci 2010-05-14 10:23:58

回答

0

主要問題在於您的上一個模板。由於dacracot points outxsl:apply-templates不需要name屬性。要調用命名模板,您可以使用xsl:call-template

如果你想申請您的SQL逃逸的所有文本節點,你可以嘗試用這樣的更換你的最後一個模板:

<xsl:template match="text()"> 
    <xsl:call-template name="sqlApostrophe"> 
    <xsl:with-param name="string" select="."/> 
    </xsl:call-template> 
</xsl:template> 

另外,爲什麼disable-output-escaping?如果文本包含特殊字符(<,>,&),則會得到格式不正確的XML作爲輸出。

+0

我試過使用它來代替我的壞模板,但仍然沒有成功 – azathoth 2010-05-13 18:33:29

+0

它是如何失敗的?也許你可以發佈一個更完整的例子。這是對我有用的東西。樣式表:http://pastebin.com/raw.php?i=vV0F2nYZ輸入:http://pastebin.com/raw.php?i=J5wLF9B0直播結果:http://www.w3.org/2000/06 /webdata/xslt?xslfile=http%3A%2F%2Fpastebin.com%2Fdownload.php%3Fi%3DvV0F2nYZ&xmlfile=http%3A%2F%2Fpastebin.com%2Fdownload.php%3Fi%3DJ5wLF9B0&transform=Submit(View Source to see the結果)。 描述: – 2010-05-13 22:03:33

0

你有幾個問題語法...

  1. 你不能有上應用模板標籤的name屬性。
  2. 您的xpath「node()| @ *」是不明確的。

你是否通過調試器運行這個?我會建議氧氣。

+0

xpath「node()| @ *」不是不明確的。 node()是'child :: node()'的簡寫,它將匹配任何其他節點的子節點。屬性不是任何其他節點的子節點,因此它們不匹配此模式。 http://www.dpawson.co.uk/xsl/sect2/nodetest.html – 2010-05-14 03:29:45

0

是否有理由調用單獨的模板進行更換?你在用什麼處理器?您應該可以在match="text()"模板中進行替換。

這工作我使用撒克遜9-HE:

輸入XML:

<?xml version="1.0" encoding="UTF-8"?> 
<name> Jim O'Connor</name> 

樣式表:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output indent="yes"/> 

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

    <xsl:template match="text()"> 
     <xsl:value-of select="replace(data(.),'''','''''')"/> 
    </xsl:template> 

</xsl:stylesheet> 

輸出XML:

<?xml version="1.0" encoding="UTF-8"?> 
<name> Jim O''Connor</name> 
+0

與氧氣進行調試時,這會帶來一些未知的錯誤NODETYPE:元素 起始位置:5:40 說明:額外的非法令牌: '元素', '(', ')', '|',' @」, '*' 開始位置:5:40 說明:找不到功能:更換 開始位置:12:62 說明:找不到功能:數據 起始位置:12:62 說明:預計,但發現:'' 開始位置:12:62 – azathoth 2010-05-14 14:32:44

+0

這取決於您在oXygen中使用的處理器。我在oXygen 11中使用了Saxon-HE,它工作正常。你可能在xygen中使用Xalan作爲你的1.0處理器。確實是 – 2010-05-14 17:25:02

+0

。 xalan是我必然要做的 – azathoth 2010-05-14 21:01:50