简体   繁体   English

XML 字段和属性的 Sql 服务器

[英]Sql Server FOR XML Fields and Attributes

I need to get an XML from a query:我需要从查询中获取 XML:

SELECT
... join ...
FOR XML PATH ('parent-lines'), ROOT('main-tag'), ELEMENTS XSINIL;

I have been able to get this structure:我已经能够得到这个结构:

<main-tag>
  <parent-lines>
    ...
    <child-name>EXAMPLE</child-name>     //case when the child is popolated
    <child-name xsi:nil=true />          //case when the child is empty
    ...
  </parent-lines>
  ...many parent-lines
</main-tag>

Now I have two problems:现在我有两个问题:

  • the first is to have the child-names without the xsi: nil attribute in case they are empty so:第一个是没有 xsi: nil 属性的子名称,以防它们为空,因此:
<child-name />
  • the second is that I have some sort of "header", static which is fine for any parent tag, which I would like to insert in the structure of my XML, and obtain a structure similar to this:第二个是我有某种“标题”,static,它适用于任何父标签,我想将其插入我的 XML 的结构中,并获得类似于以下的结构:
<main-tag>
  <header>
     ...
     <child-name>A sort of explain of the field</child-name>
     ...
  </header>                  //single header
  <parent-lines>...</parent-lines>      //many parent-lines
  ...
</main-tag>

Can you help me?你能帮助我吗?

I solved the problems, the first thanks to @Larnu and his comment, the second using this technique:我解决了这些问题,首先感谢@Larnu 和他的评论,其次是使用这种技术:

SELECT 
 (SELECT 'Cip' AS 'Cip' FOR XML PATH (''), TYPE) AS Header,
 (SELECT 'Ciop' AS 'Ciop' FOR XML PATH ('Parent-lines'), TYPE)
FOR XML PATH (''), ROOT('main-tag');

In the first nested "Select", in the PATH I have not entered anything but on the outside I baptized it with the name I wanted to obtain "AS Header".在第一个嵌套的“Select”中,在 PATH 中我没有输入任何内容,但在外部我用我想要获得“AS Header”的名称对其进行了洗礼。 In the second "Select", however, within the PATH I entered the name I wanted to repeat for each "Parent-lines".然而,在第二个“选择”中,在 PATH 中,我输入了我想为每个“父行”重复的名称。

I hope this question / answer will be useful to other users.我希望这个问题/答案对其他用户有用。 Thanks again and good luck to all!再次感谢,祝大家好运!

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

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