从父级别的节点检索值

Retreiving value from node in parent level

我无法在节点 UsagePoin/mRID 中检索值,该节点是节点 MeterReading 的子节点和节点 IntervalBlocks 的兄弟节点。

DECLARE @tbl TABLE (ID INT IDENTITY PRIMARY KEY, xml_data XML);
INSERT INTO @tbl (xml_data) VALUES
(N'<RequestMessage xmlns="http://iec.ch/TC57/2011/schema/message" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Message.xsd">
  <Header>
    <Verb>create</Verb>
    <Noun>MeterReadings</Noun>
    <Timestamp>2022-01-09T01:00:58+01:00</Timestamp>
    <Source>Y</Source>
    <AsyncReplyFlag>true</AsyncReplyFlag>
    <AckRequired>true</AckRequired>
    <MessageID>xyz_9999</MessageID>
    <CorrelationID></CorrelationID>
  </Header>
  <Payload>
    <MeterReadings xmlns="http://iec.ch/TC57/2011/MeterReadings#" xmlns:MeterReadings="http://iec.ch/TC57/2011/MeterReadings#">
      <MeterReading>
        <IntervalBlocks>
          <IntervalReadings>
            <source>999</source>
            <timeStamp>2022-01-08T00:00:00.000+01:00</timeStamp>
            <value>9.246</value>
            <ReadingQualities>
              <ReadingQualityType ref="3.0.0" />
            </ReadingQualities>
          </IntervalReadings>
          <IntervalReadings>
            <source>999</source>
            <timeStamp>2022-01-08T01:00:00.000+01:00</timeStamp>
            <value>10.404</value>
            <ReadingQualities>
              <ReadingQualityType ref="3.0.0" />
            </ReadingQualities>
          </IntervalReadings>
          <ReadingType ref="11.0.7.1.1.2.12.1.1.0.0.0.0.1010.0.3.72.0" />
        </IntervalBlocks>
        <Meter>
          <mRID>209558305</mRID>
        </Meter>
        <UsagePoint>
          <mRID>735999999999999999</mRID>
        </UsagePoint>
      </MeterReading>
      <MeterReading>
        <IntervalBlocks>
          <IntervalReadings>
            <source>999</source>
            <timeStamp>2022-01-08T00:00:00.000+01:00</timeStamp>
            <value>2.46</value>
            <ReadingQualities>
              <ReadingQualityType ref="3.0.0" />
            </ReadingQualities>
          </IntervalReadings>
          <IntervalReadings>
            <source>999</source>
            <timeStamp>2022-01-08T01:00:00.000+01:00</timeStamp>
            <value>2.52</value>
            <ReadingQualities>
              <ReadingQualityType ref="3.0.0" />
            </ReadingQualities>
          </IntervalReadings>
          <IntervalReadings>
            <source>999</source>
            <timeStamp>2022-01-08T02:00:00.000+01:00</timeStamp>
            <value>2.502</value>
            <ReadingQualities>
              <ReadingQualityType ref="3.0.0" />
            </ReadingQualities>
          </IntervalReadings>
          <ReadingType ref="11.0.7.1.1.2.12.1.1.0.0.0.0.1102.0.3.73.0" />
        </IntervalBlocks>
        <Meter>
          <mRID>999999999</mRID>
        </Meter>
        <UsagePoint>
          <mRID>735999999999999999</mRID>
        </UsagePoint>
      </MeterReading>
    </MeterReadings>
      </Payload>
</RequestMessage>');

所以我使用下面的代码。

WITH XMLNAMESPACES(N'http://iec.ch/TC57/2011/schema/message' as hdr,
                   N'http://iec.ch/TC57/2011/MeterReadings#' as mr)
SELECT
    --t.file_name,t.file_created_time received_timestamp,
    --h.value('(hdr:Timestamp)[1]', 'nvarchar(35)') AS created_timestamp,
    --h.value('(hdr:MessageID)[1]', 'nvarchar(50)') AS message_id,
    ir.value('(mr:timeStamp/text())[1]', 'nvarchar(35)') interval_timestamp,
    ir.value('(mr:value/text())[1]', 'nvarchar(35)') interval_value,
    ir.value('(mr:ReadingQualities/mr:ReadingQualityType/@ref)[1]', 'nvarchar(35)') interval_value_code
FROM
    load.capgemeni_sesp_xml t
OUTER APPLY
    t.xml_data.nodes('/hdr:RequestMessage/hdr:Header') AS m(h)
--OUTER APPLY
--    t.xml_data.nodes('/hdr:RequestMessage/hdr:Payload/mr:MeterReadings/mr:MeterReading') AS MeterReading(mr)
OUTER APPLY
    t.xml_data.nodes('/hdr:RequestMessage/hdr:Payload/mr:MeterReadings/mr:MeterReading/mr:IntervalBlocks/mr:IntervalReadings') AS IntervalReadings(ir)
where t.file_name = 'SESP_32717237.xml'
AND ir.value('(mr:timeStamp/text())[1]', 'nvarchar(35)') = '2022-01-08T00:00:00.000+01:00'

它为我提供了每个时间戳 = '2022-01-08T00:00:00.000+01:00' 的输出。现在我需要添加 UsagePoint 和 ReadingType 节点。两者都在父节点级别。

我尝试使用以下方法向上遍历层次结构

ir.value('(./mr:ReadingType/@ref)[1]','nvarchar(35)')

没用。我假设使用 ./ 会从节点 IntervalReadings 内向上遍历到节点 IntervalBlocks 内,然后它应该是 mr:ReadingType 遍历到节点 ReadingType,然后是 @ref 以获取“ref”标记中的值。

我不明白为什么它不起作用。我在从节点 mRID 获取数据时遇到类似的问题,该节点位于节点 UsagePoint 内,该节点与节点 MeterReading 内的节点 IntervalBlocks 同级。对于每个 UsagePoint 可以是 1 个或多个 ReadingType,然后具有相同级别的间隔值。

我为 ReadingType 找到的任何有效解决方案都可以用于获取 UsagePoint 的值。

编辑:为了澄清,我可以制作出看起来像这样的东西...... 它显示时间戳“2022-01-08T00:00:00.000+01:00”的值,但我无法正确添加 ReadingType 或 UsagePoint。例如,当我尝试添加 ReadingType 时,我得到空...

编辑2: 对于每个 IntervalBlocks 节点,您都有 IntervalReadings 节点,该节点然后具有 x 个间隔值,并且在与 IntervalBlocks 节点相同的级别上,您将具有值的 UsagePoint,并且在与 IntervalReadings 相同级别的 IntervalBlocks 节点内,将具有 1 个或多个 ReadingType 节点。 ReadingType 将在表示 ReadingType 的值的有效载荷之后用于该批值。 1 UsagePoint 最多可以有 4 个 ReadingType 值。 我应该通过在 XML.

中至少有 2 个 UsagePoint 和 2 个 ReadingType 来澄清数据模型

我是 运行 SQL Server 2019。 Microsoft SQL Server 2019 (RTM-CU14) (KB5007182) - 15.0.4188.2 (X64) 2021 年 11 月 3 日 19:19:51 版权所有 (C) 2019 Microsoft Corporation Enterprise Edition:基于内核的许可(64 位)在 Windows Server 2016 Standard 10.0(内部版本 14393:)(管理程序)

请尝试以下解决方案。

SQL

-- DDL and sample data population, start
DECLARE @tbl TABLE (ID INT IDENTITY PRIMARY KEY, xml_data XML);
INSERT INTO @tbl (xml_data) VALUES
(N'<RequestMessage xmlns="http://iec.ch/TC57/2011/schema/message" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Message.xsd">
    <Header>
        <Verb>create</Verb>
        <Noun>MeterReadings</Noun>
        <Timestamp>2022-01-09T01:00:58+01:00</Timestamp>
        <Source>Y</Source>
        <AsyncReplyFlag>true</AsyncReplyFlag>
        <AckRequired>true</AckRequired>
        <MessageID>xyz_9999</MessageID>
        <CorrelationID></CorrelationID>
        <Property>
            <Name>CONTRACT</Name>
            <Value>xxxx</Value>
        </Property>
        <Property>
            <Name>DIVISION</Name>
            <Value>01</Value>
        </Property>
        <Property>
            <Name>RECEIVER</Name>
            <Value>xxx</Value>
        </Property>
        <Property>
            <Name>SERVICE</Name>
            <Value>xxxx</Value>
        </Property>
    </Header>
    <Payload>
        <MeterReadings xmlns="http://iec.ch/TC57/2011/MeterReadings#" xmlns:MeterReadings="http://iec.ch/TC57/2011/MeterReadings#">
            <MeterReading>
                <IntervalBlocks>
                    <IntervalReadings>
                        <source>x</source>
                        <timeStamp>2022-01-08T00:00:00.000+01:00</timeStamp>
                        <value>9.246</value>
                        <ReadingQualities>
                            <ReadingQualityType ref="3.0.0"/>
                        </ReadingQualities>
                    </IntervalReadings>
                    <IntervalReadings>
                        <source>x</source>
                        <timeStamp>2022-01-08T01:00:00.000+01:00</timeStamp>
                        <value>10.404</value>
                        <ReadingQualities>
                            <ReadingQualityType ref="3.0.0"/>
                        </ReadingQualities>
                    </IntervalReadings>
                    <ReadingType ref="11.0.7.1.1.2.12.1.1.0.0.0.0.1010.0.3.72.0"/>
                </IntervalBlocks>
                <IntervalBlocks>
                    <IntervalReadings>
                        <source>x</source>
                        <timeStamp>2022-01-08T00:00:00.000+01:00</timeStamp>
                        <value>9.246</value>
                        <ReadingQualities>
                            <ReadingQualityType ref="3.0.0"/>
                        </ReadingQualities>
                    </IntervalReadings>
                    <IntervalReadings>
                        <source>x</source>
                        <timeStamp>2022-01-08T01:00:00.000+01:00</timeStamp>
                        <value>10.404</value>
                        <ReadingQualities>
                            <ReadingQualityType ref="3.0.0"/>
                        </ReadingQualities>
                    </IntervalReadings>
                    <ReadingType ref="11.0.7.1.1.2.12.1.1.0.0.0.0.1102.0.3.72.0"/>
                </IntervalBlocks>
                <Meter>
                    <mRID>xxxx</mRID>
                </Meter>
                <UsagePoint>
                    <mRID>zzzz</mRID>
                </UsagePoint>
            </MeterReading>
            <MeterReading>
                <IntervalBlocks>
                    <IntervalReadings>
                        <source>x</source>
                        <timeStamp>2022-01-08T00:00:00.000+01:00</timeStamp>
                        <value>9.246</value>
                        <ReadingQualities>
                            <ReadingQualityType ref="3.0.0"/>
                        </ReadingQualities>
                    </IntervalReadings>
                    <IntervalReadings>
                        <source>x</source>
                        <timeStamp>2022-01-08T01:00:00.000+01:00</timeStamp>
                        <value>10.404</value>
                        <ReadingQualities>
                            <ReadingQualityType ref="3.0.0"/>
                        </ReadingQualities>
                    </IntervalReadings>
                    <ReadingType ref="11.0.7.1.1.2.12.1.1.0.0.0.0.1010.0.3.72.0"/>
                </IntervalBlocks>
                <IntervalBlocks>
                    <IntervalReadings>
                        <source>x</source>
                        <timeStamp>2022-01-08T00:00:00.000+01:00</timeStamp>
                        <value>9.246</value>
                        <ReadingQualities>
                            <ReadingQualityType ref="3.0.0"/>
                        </ReadingQualities>
                    </IntervalReadings>
                    <IntervalReadings>
                        <source>x</source>
                        <timeStamp>2022-01-08T01:00:00.000+01:00</timeStamp>
                        <value>10.404</value>
                        <ReadingQualities>
                            <ReadingQualityType ref="3.0.0"/>
                        </ReadingQualities>
                    </IntervalReadings>
                    <ReadingType ref="11.0.7.1.1.2.12.1.1.0.0.0.0.1102.0.3.72.0"/>
                </IntervalBlocks>
                <Meter>
                    <mRID>kkkkk</mRID>
                </Meter>
                <UsagePoint>
                    <mRID>qqqqq</mRID>
                </UsagePoint>
            </MeterReading>
        </MeterReadings>
    </Payload>
</RequestMessage>');
-- DDL and sample data population, end

WITH XMLNAMESPACES(N'http://iec.ch/TC57/2011/schema/message' as hdr,
                   N'http://iec.ch/TC57/2011/MeterReadings#' as mr)
SELECT
    --t.file_name,t.file_created_time received_timestamp,
    --h.value('(hdr:Timestamp)[1]', 'nvarchar(35)') AS created_timestamp,
    --h.value('(hdr:MessageID)[1]', 'nvarchar(50)') AS message_id,
     ir.value('(mr:timeStamp/text())[1]', 'nvarchar(35)') interval_timestamp
    , ir.value('(mr:value/text())[1]', 'nvarchar(35)') interval_value
    , ir.value('(mr:ReadingQualities/mr:ReadingQualityType/@ref)[1]', 'nvarchar(35)') interval_value_code
    , mr.value('(mr:UsagePoint/mr:mRID/text())[1]', 'nvarchar(50)') UsagePoint 
    , ir.value('(../mr:ReadingType/@ref)[1]', 'nvarchar(35)') ReadingType
FROM @tbl t
OUTER APPLY t.xml_data.nodes('/hdr:RequestMessage/hdr:Header') AS m(h)
OUTER APPLY t.xml_data.nodes('/hdr:RequestMessage/hdr:Payload/mr:MeterReadings/mr:MeterReading') AS MeterReading(mr)
OUTER APPLY MeterReading.mr.nodes('mr:IntervalBlocks/mr:IntervalReadings') AS IntervalReadings(ir)
--WHERE --t.file_name = 'SESP_32717237.xml' AND 
--  ir.value('(mr:timeStamp/text())[1]', 'nvarchar(35)') = '2022-01-08T00:00:00.000+01:00'

输出

+-------------------------------+----------------+---------------------+------------+-------------------------------------+
|      interval_timestamp       | interval_value | interval_value_code | UsagePoint |             ReadingType             |
+-------------------------------+----------------+---------------------+------------+-------------------------------------+
| 2022-01-08T00:00:00.000+01:00 |          9.246 |               3.0.0 | zzzz       | 11.0.7.1.1.2.12.1.1.0.0.0.0.1010.0. |
| 2022-01-08T01:00:00.000+01:00 |         10.404 |               3.0.0 | zzzz       | 11.0.7.1.1.2.12.1.1.0.0.0.0.1010.0. |
| 2022-01-08T00:00:00.000+01:00 |          9.246 |               3.0.0 | zzzz       | 11.0.7.1.1.2.12.1.1.0.0.0.0.1102.0. |
| 2022-01-08T01:00:00.000+01:00 |         10.404 |               3.0.0 | zzzz       | 11.0.7.1.1.2.12.1.1.0.0.0.0.1102.0. |
| 2022-01-08T00:00:00.000+01:00 |          9.246 |               3.0.0 | qqqqq      | 11.0.7.1.1.2.12.1.1.0.0.0.0.1010.0. |
| 2022-01-08T01:00:00.000+01:00 |         10.404 |               3.0.0 | qqqqq      | 11.0.7.1.1.2.12.1.1.0.0.0.0.1010.0. |
| 2022-01-08T00:00:00.000+01:00 |          9.246 |               3.0.0 | qqqqq      | 11.0.7.1.1.2.12.1.1.0.0.0.0.1102.0. |
| 2022-01-08T01:00:00.000+01:00 |         10.404 |               3.0.0 | qqqqq      | 11.0.7.1.1.2.12.1.1.0.0.0.0.1102.0. |
+-------------------------------+----------------+---------------------+------------+-------------------------------------+