T-SQL,如何解析这个XML?

T-SQL, how to parse this XML?

我花了几个小时试图解析此 XML(公交车站时间表)并生成一个包含 , 的记录集。有没有办法将 XML 转换为 JSON,我觉得这样更容易处理?

有人愿意帮忙吗? (Azure SQL 服务器)

    <?xml version="1.0" encoding="UTF-8"?>
    <Trias xmlns="http://www.vdv.de/trias" version="1.1">
    <ServiceDelivery>
        <ResponseTimestamp xmlns="http://www.siri.org.uk/siri">2021-11-25T17:52:12Z</ResponseTimestamp>
        <DeliveryPayload>
            <StopEventResponse>
                <StopEventResult>
                    <StopEvent>
                        <ThisCall>
                            <CallAtStop>
                                <ServiceDeparture>
                                    <TimetabledTime>2021-11-25T17:53:00Z</TimetabledTime>
                                    <EstimatedTime>2021-11-25T17:53:00Z</EstimatedTime>
                                </ServiceDeparture>
                            </CallAtStop>
                        </ThisCall>
                        <Service>
                            <PublishedLineName>
                                <Text>58</Text>
                                <Language>de</Language>
                            </PublishedLineName>
                        </Service>
                    </StopEvent>
                </StopEventResult>
                <StopEventResult>
                    <StopEvent>
                        <ThisCall>
                            <CallAtStop>
                                <ServiceDeparture>
                                    <TimetabledTime>2021-11-25T17:58:00Z</TimetabledTime>
                                    <EstimatedTime>2021-11-25T17:58:00Z</EstimatedTime>
                                </ServiceDeparture>
                            </CallAtStop>
                        </ThisCall>
                        <Service>
                            <PublishedLineName>
                                <Text>60</Text>
                                <Language>de</Language>
                            </PublishedLineName>
                        </Service>
                    </StopEvent>
                </StopEventResult>
            </StopEventResponse>
        </DeliveryPayload>
    </ServiceDelivery>
</Trias>

未提供可重现的最小示例。

所以从臀部射击。

不需要任何 XML 解析。 SQL 服务器自带内置 XQuery 语言支持来处理 XML 数据类型。

唯一的细微差别是输入 XML 有命名空间。

  • 使用 XMLNAMESPACES() 子句声明默认命名空间。
  • 正在使用几个 XQuery 方法:.nodes().value()

SQL

DECLARE @xml XML =
N'<Trias xmlns="http://www.vdv.de/trias" version="1.1">
    <ServiceDelivery>
        <ResponseTimestamp xmlns="http://www.siri.org.uk/siri">2021-11-25T17:52:12Z</ResponseTimestamp>
        <DeliveryPayload>
            <StopEventResponse>
                <StopEventResult>
                    <StopEvent>
                        <ThisCall>
                            <CallAtStop>
                                <ServiceDeparture>
                                    <TimetabledTime>2021-11-25T17:53:00Z</TimetabledTime>
                                    <EstimatedTime>2021-11-25T17:53:00Z</EstimatedTime>
                                </ServiceDeparture>
                            </CallAtStop>
                        </ThisCall>
                        <Service>
                            <PublishedLineName>
                                <Text>58</Text>
                                <Language>de</Language>
                            </PublishedLineName>
                        </Service>
                    </StopEvent>
                </StopEventResult>
                <StopEventResult>
                    <StopEvent>
                        <ThisCall>
                            <CallAtStop>
                                <ServiceDeparture>
                                    <TimetabledTime>2021-11-25T17:58:00Z</TimetabledTime>
                                    <EstimatedTime>2021-11-25T17:58:00Z</EstimatedTime>
                                </ServiceDeparture>
                            </CallAtStop>
                        </ThisCall>
                        <Service>
                            <PublishedLineName>
                                <Text>60</Text>
                                <Language>de</Language>
                            </PublishedLineName>
                        </Service>
                    </StopEvent>
                </StopEventResult>
            </StopEventResponse>
        </DeliveryPayload>
    </ServiceDelivery>
</Trias>';

;WITH XMLNAMESPACES(DEFAULT 'http://www.vdv.de/trias')
SELECT c.value('(ThisCall/CallAtStop/ServiceDeparture/TimetabledTime/text())[1]', 'DATETIMEOFFSET(0)') AS TimetabledTime
    , c.value('(ThisCall/CallAtStop/ServiceDeparture/EstimatedTime/text())[1]', 'DATETIMEOFFSET(0)') AS EstimatedTime
    , c.value('(Service/PublishedLineName/Text/text())[1]', 'VARCHAR(100)') AS [Text]
    , c.value('(Service/PublishedLineName/Language/text())[1]', 'CHAR(2)') AS [Language]
FROM @xml.nodes('/Trias/ServiceDelivery/DeliveryPayload/StopEventResponse/StopEventResult/StopEvent') AS t(c);

输出

+----------------------------+----------------------------+------+----------+
|       TimetabledTime       |       EstimatedTime        | Text | Language |
+----------------------------+----------------------------+------+----------+
| 2021-11-25 17:53:00 +00:00 | 2021-11-25 17:53:00 +00:00 |   58 | de       |
| 2021-11-25 17:58:00 +00:00 | 2021-11-25 17:58:00 +00:00 |   60 | de       |
+----------------------------+----------------------------+------+----------+