SQL Server:选择具有HTML值的XML字段
我为一个网站预先构建了一个ecomm模块,该网站将HTML存储在SQL的XML字段中 也就是说,当我从该字段中选择数据时,它都是行话,而不是HTML 如何在select语句中将XML重新编码为HTML 存储在xml字段中的数据[概述]:SQL Server:选择具有HTML值的XML字段,html,sql-server,xml,tsql,Html,Sql Server,Xml,Tsql,我为一个网站预先构建了一个ecomm模块,该网站将HTML存储在SQL的XML字段中 也就是说,当我从该字段中选择数据时,它都是行话,而不是HTML 如何在select语句中将XML重新编码为HTML 存储在xml字段中的数据[概述]: <locale en-US="<h3>As Shown Details</h3> <p>6514/1 SN AWH TABLE LAMP AS SHOWN</
<locale en-US="<h3>As Shown Details</h3> <p>6514/1 SN AWH TABLE LAMP AS SHOWN</p> <h3>Item Details</h3> <div class="table-responsive"> <table class="table table-striped table-condensed" width="100%" border="0"> <tbody> <tr> <th scope="row">Manufacturer</th> <td>Holtkotter International</td> </tr> <tr> <th scope="row">Dimensions</th> <td>Width 7.25 x Depth 7.25 x Height 18.5</td> </tr> <tr> <th scope="row">Seat Height</th> <td></td> </tr> <tr> <th scope="row">Arm Height</th> <td></td> </tr> <tr> <th scope="row">Inside Depth</th> <td></td> </tr> <tr> <th scope="row">Fabric Content</th> <td></td> </tr> <tr> <th scope="row">Country of Origin</th> <td></td> </tr> </tbody> </table> </div> <div class="part1">This modern table lamp adds style and versatility to virtually any decor. Equipped with a full-range, turn-knob dimmer and a 100 Watt Halogen bulb by Osram. Pair it with the matching wall sconce 9426, floor lamp 6515, or swing-arm floor lamp 9434.</div><br><br><div class="part2">Available in Hand Brushed Old Bronze (shown), Antique Brass, Brushed Brass, Chrome, and Satin Nickel Finishes.</div><br><br><div class="part3">Halogen Line Voltage 100W bulb included.</div>" />
但它又回来了
<code><locale en-US="<h3>As Shown Details</h3> <p>6514/1 SN AWH TABLE LAMP AS SHOWN</p> <h3>Item Details</h3> <div class="table-responsive"> <table class="table table-striped table-condensed" width="100%" border="0"> <tbody> <tr> <th scope="row">Manufacturer</th> <td>Holtkotter International</td> </tr> <tr> <th scope="row">Dimensions</th> <td>Width 7.25 x Depth 7.25 x Height 18.5</td> </tr> <tr> <th scope="row">Seat Height</th> <td></td> </tr> <tr> <th scope="row">Arm Height</th> <td></td> </tr> <tr> <th scope="row">Inside Depth</th> <td></td> </tr> <tr> <th scope="row">Fabric Content</th> <td></td> </tr> <tr> <th scope="row">Country of Origin</th> <td></td> </tr> </tbody> </table> </div> <div class="part1">This modern table lamp adds style and versatility to virtually any decor. Equipped with a full-range, turn-knob dimmer and a 100 Watt Halogen bulb by Osram. Pair it with the matching wall sconce 9426, floor lamp 6515, or swing-arm floor lamp 9434.</div><br><br><div class="part2">Available in Hand Brushed Old Bronze (shown), Antique Brass, Brushed Brass, Chrome, and Satin Nickel Finishes.</div><br><br><div class="part3">Halogen Line Voltage 100W bulb included.</div>" /></code>
无需手动重新编码。直接从XML读取属性值将隐式地为您执行所有重新编码:
DECLARE @mockup TABLE(Overview XML);
INSERT INTO @mockup(Overview)
VALUES(N'<locale en-US="<h3>As Shown Details</h3> <p>6514/1 SN AWH TABLE LAMP AS SHOWN</p> <h3>Item Details</h3> <div class="table-responsive"> <table class="table table-striped table-condensed" width="100%" border="0"> <tbody> <tr> <th scope="row">Manufacturer</th> <td>Holtkotter International</td> </tr> <tr> <th scope="row">Dimensions</th> <td>Width 7.25 x Depth 7.25 x Height 18.5</td> </tr> <tr> <th scope="row">Seat Height</th> <td></td> </tr> <tr> <th scope="row">Arm Height</th> <td></td> </tr> <tr> <th scope="row">Inside Depth</th> <td></td> </tr> <tr> <th scope="row">Fabric Content</th> <td></td> </tr> <tr> <th scope="row">Country of Origin</th> <td></td> </tr> </tbody> </table> </div> <div class="part1">This modern table lamp adds style and versatility to virtually any decor. Equipped with a full-range, turn-knob dimmer and a 100 Watt Halogen bulb by Osram. Pair it with the matching wall sconce 9426, floor lamp 6515, or swing-arm floor lamp 9434.</div><br><br><div class="part2">Available in Hand Brushed Old Bronze (shown), Antique Brass, Brushed Brass, Chrome, and Satin Nickel Finishes.</div><br><br><div class="part3">Halogen Line Voltage 100W bulb included.</div>" />')
SELECT m.Overview.value(N'(/locale/@en-US)[1]','nvarchar(max)')
FROM @mockup AS m
DECLARE@mockup表(概述XML);
插入@mockup(概述)
值(N“”)
选择m.Overview.value(N'(/locale/@en-US)[1],'nvarchar(max)'
来自@mockup AS m
由于HTML规则不太严格(即未关闭的
元素),因此无法将结果转换为XML。但它工作得很好。嵌入到一个网站中,浏览器向我显示了这一点(重要您的CSS类当然丢失了…):
无需进行任何手动重新编码。直接从XML读取属性值将隐式地为您执行所有重新编码:
DECLARE @mockup TABLE(Overview XML);
INSERT INTO @mockup(Overview)
VALUES(N'<locale en-US="<h3>As Shown Details</h3> <p>6514/1 SN AWH TABLE LAMP AS SHOWN</p> <h3>Item Details</h3> <div class="table-responsive"> <table class="table table-striped table-condensed" width="100%" border="0"> <tbody> <tr> <th scope="row">Manufacturer</th> <td>Holtkotter International</td> </tr> <tr> <th scope="row">Dimensions</th> <td>Width 7.25 x Depth 7.25 x Height 18.5</td> </tr> <tr> <th scope="row">Seat Height</th> <td></td> </tr> <tr> <th scope="row">Arm Height</th> <td></td> </tr> <tr> <th scope="row">Inside Depth</th> <td></td> </tr> <tr> <th scope="row">Fabric Content</th> <td></td> </tr> <tr> <th scope="row">Country of Origin</th> <td></td> </tr> </tbody> </table> </div> <div class="part1">This modern table lamp adds style and versatility to virtually any decor. Equipped with a full-range, turn-knob dimmer and a 100 Watt Halogen bulb by Osram. Pair it with the matching wall sconce 9426, floor lamp 6515, or swing-arm floor lamp 9434.</div><br><br><div class="part2">Available in Hand Brushed Old Bronze (shown), Antique Brass, Brushed Brass, Chrome, and Satin Nickel Finishes.</div><br><br><div class="part3">Halogen Line Voltage 100W bulb included.</div>" />')
SELECT m.Overview.value(N'(/locale/@en-US)[1]','nvarchar(max)')
FROM @mockup AS m
DECLARE@mockup表(概述XML);
插入@mockup(概述)
值(N“”)
选择m.Overview.value(N'(/locale/@en-US)[1],'nvarchar(max)'
来自@mockup AS m
由于HTML规则不太严格(即未关闭的
元素),因此无法将结果转换为XML。但它工作得很好。嵌入到一个网站中,浏览器向我显示了这一点(重要您的CSS类当然丢失了…):
也许一些样本数据和期望的结果会有帮助@johncapelletti我刚刚添加了一些数据也许一些样本数据和期望的结果会有帮助@johncapelletti我刚刚添加了一些data@JohnCappelletti一个(相关的)提示。无需使用
REPLACE
对实体重新编码。试试这个:DECLARE@s VARCHAR(100)='somevilxml attr=“this&;that”/;选择CAST('+@s+''作为XML.value(N'.'和'nvarchar(max)')代码>。主要优点:如果存在意外的实体,您的REPLACE
cascade将无法捕获它……我现在明白了。我的第一次尝试失败了,所以我硬着头皮强迫它。我真的应该花一点时间再考虑一下。我会告诉我的孩子(和工作人员),“没有必要匆忙给出错误的答案。”@johncapelletti-One(相关)提示。无需使用REPLACE
对实体重新编码。试试这个:DECLARE@s VARCHAR(100)='somevilxml attr=“this&;that”/;选择CAST('+@s+''作为XML.value(N'.'和'nvarchar(max)')代码>。主要优点:如果存在意外的实体,您的REPLACE
cascade将无法捕获它……我现在明白了。我的第一次尝试失败了,所以我硬着头皮强迫它。我真的应该花一点时间再考虑一下。我会告诉我的孩子(和员工),“没有必要急于给出错误的答案。”