我有一个富文本编辑器,可以将我的标记保存到数据库中。我有一个使用XML Auto的存储过程来恢复该数据。您可能已经猜到的问题是,XML Auto会对我的标记进行编码,因此富文本不会显示在我的网页显示上。只有标记文字显示。
XML Auto中有什么东西可以防止某些字段被编码吗?欢迎其他想法。
XML
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
XSL
<?xml version="1.0" encoding="utf-8"?>
<catalog>
<cd>
<album>Thriller</album>
<artist>Michael Jackson</artist>
<notes><strong>test</strong></notes>
</cd>
<cd>
<album>Album2</album>
<artist>Artist2</artist>
<notes>Can be plain text as well.</notes>
</cd>
</catalog>
c#XSLT
string xmlFile = @"XMLFile.xml"; //<view>html column</view>
string xslFile = @"XSLTFile.xslt"; //views.xsl file
string xmlFilePath = Request.PhysicalApplicationPath + xmlFile;
string xsltPath = Request.PhysicalApplicationPath + xslFile;
XPathDocument xPathDoc = new XPathDocument(xmlFilePath);
XslCompiledTransform transform = new XslCompiledTransform();
transform.Load(xsltPath);
transform.Transform(xPathDoc, null, Response.Output);
答案 0 :(得分:2)
未支持 reserved chars的“XML”是非法的。所以不可能有像<root>a&b</root>
这样的XML,因为保留了&
个字符。解决方案是在使用前解码字符串:
DECLARE @x XML;
SET @x = (SELECT 'a&b' AS Col FOR XML RAW)
SELECT @x AS StringEncoded
SELECT @x.value('(/row/@Col)[1]', 'NVARCHAR(50)') AS StringDecoded
结果:
StringEncoded
---------------------
<row Col="a&b" />
StringDecoded
---------------------
a&b
第二个例子:
DECLARE @x2 XML;
SET @x2 = (SELECT '<strong>test</strong>' AS Col FOR XML RAW)
SELECT @x2 AS StringEncoded2
SELECT @x2.value('(/row/@Col)[1]', 'NVARCHAR(50)') AS StringDecoded2
结果:
StringEncoded2
-----------------------------------------------
<row Col="<strong>test</strong>" />
(1 row(s) affected)
StringDecoded2
-----------------------------------------------
<strong>test</strong>