简体   繁体   English

选择XML仅在几个具有相同名称的节点上多个

[英]Select XML multiple only a few nodes with the same name

I'm trying to construct a soap message, and I was able to construct the entire message using a single select. 我正在尝试构造一条肥皂消息,并且能够使用单个选择构造整个消息。 Except the problem is, on only a few occasions the same node name is repeated twice. 除非问题是,仅在少数情况下,相同的节点名称会重复两次。

So for example the required output result should be like so, with two separate id root nodes: 因此,例如,所需的输出结果应该像这样,带有两个单独的id root

<SoapDocument>
  <recordTarget>
    <patientRole>
      <id root="1.2.3.4" extension="1234567" />
      <id root="1.2.3.5.6" extension="0123456789" />
    </patientRole>
  </recordTarget>
</SoapDocument>

I tried to use my sparse knowledge of xpath to construct the node names like so: 我试图用我对xpath的稀疏知识来构造节点名称,如下所示:

select
    '1.2.3.4'    AS 'recordTarget/patientRole/id[1]/@root',
    '1234567'    AS 'recordTarget/patientRole/id[1]/@extension',
    '1.2.3.5.6'  AS 'recordTarget/patientRole/id[2]/@root',
    '0123456789' AS 'recordTarget/patientRole/id[2]/@extension'
FOR XML PATH('SoapDocument'),TYPE

Apparently xpath naming can't be applied to column names id[1] and id[2] like that? 显然,xpath命名不能像这样应用于列名id[1]id[2]吗? Am I missing something here or should the notation be different? 我是否在这里遗漏了一些东西,还是应该使用不同的符号? What would be the easiest way to constuct the desired result? 构造所需结果的最简单方法是什么?

From your question I assume, this is not tabular data, but fixed values and you are creating a medical document, assumably a CDA. 根据您的问题,我认为这不是表格数据,而是固定值,您正在创建医疗文档,大概是CDA。

Try this: 尝试这个:

SELECT 
(
    SELECT
        '1.2.3.4'    AS 'id/@root',
        '1234567'    AS 'id/@extension',
        '',
        '1.2.3.5.6'  AS 'id/@root',
        '0123456789' AS 'id/@extension'
    FOR XML PATH('patientRole'),TYPE
) AS [SoapDocument/recordTarget]
FOR XML PATH('')

The result: 结果:

<SoapDocument>
  <recordTarget>
    <patientRole>
      <id root="1.2.3.4" extension="1234567" />
      <id root="1.2.3.5.6" extension="0123456789" />
    </patientRole>
  </recordTarget>
</SoapDocument>

Some explanation: The empty element in the middle allows you to place two elements with the same name in one query. 一些解释:中间的空元素允许您在一个查询中放置两个具有相同名称的元素。 There are various approaches how you get this into your surrounding tags. 您可以通过多种方法将其添加到周围的标签中。 This is just one possibility. 这只是一种可能性。

UPDATE UPDATE

I'd like to point to BdR's own answer! 我想指出BdR的答案! Great finding and worth an up-vote! 很棒的发现,值得投票!

A little more elaboration on the answer from Shnugo, as it got me trying out some things using an "empty column". 关于Shnugo答案的详细说明,这使我使用“空列”尝试了一些操作。

If you do not give the emtpy column a name, it will reset to the XML root node. 如果您没有给emtpy列命名,它将重置为XML根节点。 So the following columns will start from the XML root of the selection you are in at that point. 因此,以下各列将从您当时所选择的XML根目录开始。 However, if you explicitly name the empty separator column, then the following columns will continue in the hierarchy as set by that column name. 但是,如果您明确命名为空的分隔符列,则随后的列将在该列名设置的层次结构中继续。

So the selection below will also result in the desired result. 因此,下面的选择也将产生所需的结果。 It's subtly different, but in my case it allows me to avoid using subselections. 它有些微的不同,但是在我的情况下,它使我避免使用子选择。

select
    '1.2.3.4'    AS 'recordTarget/patientRole/id/@root',
    '1234567'    AS 'recordTarget/patientRole/id/@extension',
    ''           AS 'recordTarget/patientRole',
    '1.2.3.5.6'  AS 'recordTarget/patientRole/id/@root',
    '0123456789' AS 'recordTarget/patientRole/id/@extension'
FOR XML PATH('SoapDocument'),TYPE

This should do the job: 这应该做的工作:

WITH CTE AS (
    SELECT *
    FROM (VALUES('1.2.3.4','1234567'),
                ('1.2.3.5.6','0123456789')) V ([root], [extension]))
SELECT (SELECT (SELECT (SELECT [root] AS [@root],
                               [extension] AS [@extension]
                        FROM CTE
                        FOR XML PATH('id'), TYPE)
                FOR XML PATH('patientRole'), TYPE)
        FOR XML PATH ('recordTarget'), TYPE)
FOR XML PATH ('SoapDocument');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM