如何 select html 文档中的文本值 sql?
How to select Text value from html document in sql?
我想 select li 1 ta 3 的所有值(例如)。但我不能。
我的代码底部和问题结束时的最终输出。
代码:
Declare @XML1 xml = N'
<div>
<div>
<h4>انتخاب رنگ</h4>
<ul id="frmErblProductColor" autocompelete="off">
<li>
<div id="frmErblProductColor_ContainerItem_0" class="DkProductColorBox">
<input id="frmErblProductColor_RadioItem_0" type="radio" name="ctl29$frmErblProductColor$ProductColorGroup" value="1" checked="checked"/>
<label id="frmErblProductColor_LabelItem_0" for="frmErblProductColor_RadioItem_0" data-color="#2b2b2b">مشکی</label>
</div>
</li>
<li>
<div id="frmErblProductColor_ContainerItem_1" class="DkProductColorBox">
<input id="frmErblProductColor_RadioItem_1" type="radio" name="ctl29$frmErblProductColor$ProductColorGroup" value="4"/>
<label id="frmErblProductColor_LabelItem_1" for="frmErblProductColor_RadioItem_1" data-color="blue">آبی</label>
</div>
</li>
<li>
<div id="frmErblProductColor_ContainerItem_2" class="DkProductColorBox">
<input id="frmErblProductColor_RadioItem_2" type="radio" name="ctl29$frmErblProductColor$ProductColorGroup" value="16"/>
<label id="frmErblProductColor_LabelItem_2" for="frmErblProductColor_RadioItem_2" data-color="darkred">زرشکی</label>
</div>
</li>
</ul>
</div>
<div></div>
<ul></ul>
</div>';
SELECT
t.v.value('(div[1]/h4/text())[1]','nvarchar(max)') AS [Selection]
,t.v.value('(ul/li/div/label/text())[1]','nvarchar(max)') AS [Value]
,t.v.value('(ul/li/div/input/@value)[1]','nvarchar(max)') AS [Code]
FROM
@XML1.nodes('div/div[1]') as t(v)
SELECT
c.d.value('.','nvarchar(max)') AS [Selection]
,t.v.value('.','nvarchar(max)') AS [Value]
,f.j.value('@value','nvarchar(max)') AS [Code]
FROM
@XML1.nodes('div/div[1]/div[1]/h4') as c(d)
cross apply @XML1.nodes('div/div[1]/ul/li/div/label') as t(v)
cross apply @XML1.nodes('div/div[1]/ul/li/div/input') as f(j)
输出最终必须:
[Code] [Selection] [Value]
-------------- -------------- -----------
مشکی انتخاب رنگ 1
زرشکی انتخاب رنگ 16
آبی انتخاب رنگ 4
现在无法使用此代码。
SELECT
t.v.value('(input/@value)[1]','nvarchar(max)') AS [Code]
,@XML1.value('(div/div/h4/text())[1]','nvarchar(max)') AS [Selection]
,t.v.value('(label/text())[1]','nvarchar(max)') AS [Value]
FROM
@XML1.nodes('div/div/ul/li/div') as t(v)
或者这个更漂亮:
SELECT
[Selection]
,t.v.value('(ul/li/div/label/text())[1]','nvarchar(max)') AS [Value]
,t.v.value('(ul/li/div/input/@value)[1]','nvarchar(max)') AS [Code]
,t.v.query('.')
FROM
@XML1.nodes('div/div[1]') as t(v)
outer apply (select @XML1.value('(div/div/h4/text())[1]','nvarchar(max)') AS [Selection]) a
我想 select li 1 ta 3 的所有值(例如)。但我不能。 我的代码底部和问题结束时的最终输出。
代码:
Declare @XML1 xml = N'
<div>
<div>
<h4>انتخاب رنگ</h4>
<ul id="frmErblProductColor" autocompelete="off">
<li>
<div id="frmErblProductColor_ContainerItem_0" class="DkProductColorBox">
<input id="frmErblProductColor_RadioItem_0" type="radio" name="ctl29$frmErblProductColor$ProductColorGroup" value="1" checked="checked"/>
<label id="frmErblProductColor_LabelItem_0" for="frmErblProductColor_RadioItem_0" data-color="#2b2b2b">مشکی</label>
</div>
</li>
<li>
<div id="frmErblProductColor_ContainerItem_1" class="DkProductColorBox">
<input id="frmErblProductColor_RadioItem_1" type="radio" name="ctl29$frmErblProductColor$ProductColorGroup" value="4"/>
<label id="frmErblProductColor_LabelItem_1" for="frmErblProductColor_RadioItem_1" data-color="blue">آبی</label>
</div>
</li>
<li>
<div id="frmErblProductColor_ContainerItem_2" class="DkProductColorBox">
<input id="frmErblProductColor_RadioItem_2" type="radio" name="ctl29$frmErblProductColor$ProductColorGroup" value="16"/>
<label id="frmErblProductColor_LabelItem_2" for="frmErblProductColor_RadioItem_2" data-color="darkred">زرشکی</label>
</div>
</li>
</ul>
</div>
<div></div>
<ul></ul>
</div>';
SELECT
t.v.value('(div[1]/h4/text())[1]','nvarchar(max)') AS [Selection]
,t.v.value('(ul/li/div/label/text())[1]','nvarchar(max)') AS [Value]
,t.v.value('(ul/li/div/input/@value)[1]','nvarchar(max)') AS [Code]
FROM
@XML1.nodes('div/div[1]') as t(v)
SELECT
c.d.value('.','nvarchar(max)') AS [Selection]
,t.v.value('.','nvarchar(max)') AS [Value]
,f.j.value('@value','nvarchar(max)') AS [Code]
FROM
@XML1.nodes('div/div[1]/div[1]/h4') as c(d)
cross apply @XML1.nodes('div/div[1]/ul/li/div/label') as t(v)
cross apply @XML1.nodes('div/div[1]/ul/li/div/input') as f(j)
输出最终必须:
[Code] [Selection] [Value]
-------------- -------------- -----------
مشکی انتخاب رنگ 1
زرشکی انتخاب رنگ 16
آبی انتخاب رنگ 4
现在无法使用此代码。
SELECT
t.v.value('(input/@value)[1]','nvarchar(max)') AS [Code]
,@XML1.value('(div/div/h4/text())[1]','nvarchar(max)') AS [Selection]
,t.v.value('(label/text())[1]','nvarchar(max)') AS [Value]
FROM
@XML1.nodes('div/div/ul/li/div') as t(v)
或者这个更漂亮:
SELECT
[Selection]
,t.v.value('(ul/li/div/label/text())[1]','nvarchar(max)') AS [Value]
,t.v.value('(ul/li/div/input/@value)[1]','nvarchar(max)') AS [Code]
,t.v.query('.')
FROM
@XML1.nodes('div/div[1]') as t(v)
outer apply (select @XML1.value('(div/div/h4/text())[1]','nvarchar(max)') AS [Selection]) a