首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何对xml中不同的元素节点求和,以便在另一个节点中赋值

如何对xml中不同的元素节点求和,以便在另一个节点中赋值
EN

Stack Overflow用户
提问于 2020-04-20 15:09:23
回答 1查看 35关注 0票数 0

我在sql server中有一个xml,它是这样的:

代码语言:javascript
运行
复制
<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。

EN

回答 1

Stack Overflow用户

发布于 2020-04-20 16:12:44

我猜有一种简单的方法可以做到这一点,但这里有一个变体:

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
(./Student[@version="2"]/Section[@name="Report"]/InputNumber[@description="Total  of all subjects marks added together."]/Value)[1]

文本很简单,这里有几个:

代码语言:javascript
运行
复制
sum(./Student[@version="2"]/Section[@name="Report"]/InputNumber[@description="Marks out of 100"]/Value)

我不喜欢使用description标记值来选择节点。如果你有别的办法就更好了。

此外,如果您在SQL表中对这些数据进行规范化,并在将其发送到应用程序之前使用FOR XML子句构建此XML,则会更好。

您可以像这样过滤节点:

代码语言:javascript
运行
复制
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]');

使用标题:

代码语言:javascript
运行
复制
(./Student[@version="2"]/Section[@name="Report"]/InputNumber[@title="Maths" or @title="Science" or @title="English"]/Value)

最好是添加一种类型的属性并按其进行过滤-例如,添加input type=mark

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61316868

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档