我在sql server中有一个xml,它是这样的:
<Student version="2">
<Section name="Report">
<Glossary>
<Item name="Some text"</Item>
</Glossary>
<InputNumber type="int" min="0" max="100" title="Maths" format="normal" description="Marks obtained in Maths out of 100">
<Value>70</Value>
</InputNumber>
<InputNumber type="int" min="0" max="100" title="Science" format="normal" description="Marks obtained in Science out of 100">
<Value>60</Value>
</InputNumber>
<InputNumber type="int" min="0" max="100" title="English" format="normal" description="Marks obtained in English out of 100">
<Value>80</Value>
</InputNumber>
<InputNumber type="float" min="100" max="100" title="Total " format="normal" description="Total of all subjects marks added together.">
<Value/>
</InputNumber>
<InputNumber type="int" min="0" max="10000" title="How many students in the class?" format="normal" description="total students>
<Value>19</Value>
</InputNumber>
<InputNumber type="int" min="0" max="100" title="How many subjects are there?" format="normal" description="total subjects">
<Value>3</Value>
</InputNumber>
</Section>
<Section>
....
</Section>
</Student>
这里,在/Student1/Section1/InputNumber4的值中,所有主题中的所有标记的总和将被填充,在本例中将为210。
如何求和节点中的值: /Student1/Section1/InputNumber1、/Student1/Section1/InputNumber2、/Student1/Section1/InputNumber3并将其分配给/Student1/Section1/InputNumber4。
发布于 2020-04-20 08:12:44
我猜有一种简单的方法可以做到这一点,但这里有一个变体:
DECLARE @DataXML XML;
SET @DataXML = '<Student version="2">
<Section name="Report">
<Glossary>
<Item name="Some text"></Item>
</Glossary>
<InputNumber type="int" min="0" max="100" title="Maths" format="normal" description="Marks out of 100">
<Value>70</Value>
</InputNumber>
<InputNumber type="int" min="0" max="100" title="Science" format="normal" description="Marks out of 100">
<Value>60</Value>
</InputNumber>
<InputNumber type="int" min="0" max="100" title="English" format="normal" description="Marks out of 100">
<Value>80</Value>
</InputNumber>
<InputNumber type="float" min="100" max="100" title="Total " format="normal" description="Total of all subjects marks added together.">
<Value />
</InputNumber>
<InputNumber type="int" min="0" max="10000" title="How many students in the class?" format="normal" description="total students">
<Value>19</Value>
</InputNumber>
<InputNumber type="int" min="0" max="100" title="How many subjects are there?" format="normal" description="total subjects">
<Value>3</Value>
</InputNumber>
</Section>
</Student>';
SET @DataXML.modify('insert text{sum(./Student[@version="2"]/Section[@name="Report"]/InputNumber[@description="Marks out of 100"]/Value)} into (./Student[@version="2"]/Section[@name="Report"]/InputNumber[@description="Total of all subjects marks added together."]/Value)[1]');
SELECT @DataXML;
我们的想法是为这个节点插入text
:
(./Student[@version="2"]/Section[@name="Report"]/InputNumber[@description="Total of all subjects marks added together."]/Value)[1]
文本很简单,这里有几个:
sum(./Student[@version="2"]/Section[@name="Report"]/InputNumber[@description="Marks out of 100"]/Value)
我不喜欢使用description标记值来选择节点。如果你有别的办法就更好了。
此外,如果您在SQL表中对这些数据进行规范化,并在将其发送到应用程序之前使用FOR XML
子句构建此XML
,则会更好。
您可以像这样过滤节点:
SET @DataXML.modify('insert text{sum(./Student[@version="2"]/Section[@name="Report"]/InputNumber[@title="Maths" or @title="Science" or @title="English"]/Value)} into (./Student[@version="2"]/Section[@name="Report"]/InputNumber[@description="Total of all subjects marks added together."]/Value)[1]');
使用标题:
(./Student[@version="2"]/Section[@name="Report"]/InputNumber[@title="Maths" or @title="Science" or @title="English"]/Value)
最好是添加一种类型的属性并按其进行过滤-例如,添加input
type=mark
。
https://stackoverflow.com/questions/61316868
复制相似问题