SQL OpenXML多标签问题如何处理?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (18)

我是新手,通过SQL Server Management Studio将XML文件读入表中。可能有更好的方法,但我想用这种方法。

目前我正在阅读标准的人员记录XML文件。一个<record>标签是每行数据的最高水平。我想将所有记录读入我的SQL表中单独的行。

到目前为止,我使用以下方法得到了很好的结果,如下所示:

SELECT
        -- Record
        category, editor, entered, subcategory, uid, updated,
        -- Person
        first_name, last_name, ssn, ei, title, POSITION,
FROM OPENXML(@hDoc, 'records/record/person/names')
WITH 
(
    -- Record
    category [varchar](100) '../../@category',
    editor [varchar](100) '../../@editor',
    entered Datetime '../../@entered',
    subcategory [varchar](100) '../../@subcategory',
    uid BIGINT '../../@uid',
    updated [varchar](100) '../../@updated',
    -- Person
    first_name [varchar](100) 'first_name',
    last_name [varchar](100) 'last_name',
    ssn [varchar](100) '../@ssn',
    ei [varchar](100) '../@e-i',
    title [varchar](100) '../title',
    Position [varchar](100) '../position',  
)

然而这种方法工作得很好,因为标签名称对于每个记录/人员都是唯一的。我遇到的问题是在<Person>标签内,我现在有一个<Aliases>标签,其中包含多个<Alias> test name </Alias>标签的列表。如果我使用上面的方法&reference '../aliases',我将所有的Alias元素作为一个长的String行混合在一起。如果我只是尝试“../aliases/alias”,则每个记录行返回第一个元素。如果在Aliases标签集中有10个Alias元素,我想要返回10行作为例子。

有没有一种方法可以指定当更高级别标记中有多个相同名称的标记时,将它们全部返回而不仅仅是一行?

以下是我所指的XML中的示例块:

- <aliases>
  <alias>test 1</alias> 
  <alias>test 2</alias> 
  <alias>test 3</alias> 
  </aliases>

我想在SQL表中的以下内容:

Record               Aliases

Record 1             test 1
Record 1             test 2
Record 1             test 3
Record 2             test 4
Record 2             test 5

但我得到的是:

Record 1             test 1 
Record 2             test 4
提问于
用户回答回答于

尝试修改FROM子句如下:

SELECT
...

FROM OPENXML(@hDoc, 'records/record/person/aliases')
用户回答回答于

/* Please note I have used different cases than you to follow a standard (first letter capitialised for elements, lowercase for attributes.
   As xml is case sensitive, you may need to change the below code to suit the case of your data */

/* Bring your xml into an xml variable */

DECLARE @xml xml = '
<Records>
    <Record category="category1" editor="editor1" entered="2015-01-01" subcategory="subcategory1" uid="100001" updated="updated1">
        <Person ssn="ssn1" e-i="ei1">
            <Title>Title1</Title>
            <Position>Position1</Position>
            <Names>
                <First_name>FirstName1</First_name>
                <Last_name>LastName1</Last_name>
                <Aliases>
                    <Alias>test1</Alias>
                    <Alias>test2</Alias>
                    <Alias>test3</Alias>
                </Aliases>
            </Names>
        </Person>
    </Record>
    <Record category="category2" editor="editor2" entered="2015-01-02" subcategory="subcategory2" uid="100002" updated="updated2">
        <Person ssn="ssn2" e-i="ei2">
            <Title>Title2</Title>
            <Position>Position2</Position>
            <Names>
                <First_name>FirstName2</First_name>
                <Last_name>LastName2</Last_name>
                <Aliases>
                    <Alias>test4</Alias>
                    <Alias>test5</Alias>
                </Aliases>
            </Names>
        </Person>
    </Record>
</Records>'


/* The unary relationship of 1 element/attribute type per record in xpath */

SELECT T.rows.value('@category[1]', '[varchar](100)') AS Category
      ,T.rows.value('@editor[1]', '[varchar](100)') AS Editor
      ,T.rows.value('@entered[1]', '[Datetime]') AS Entered
      ,T.rows.value('@subcategory[1]', '[varchar](100)') AS Subcategory
      ,T.rows.value('@uid[1]', '[bigint]') AS [UID]
      ,T.rows.value('@updated[1]', '[varchar](100)') AS Updated
      ,T.rows.value('(Person/Names/First_name)[1]', '[varchar](100)') AS First_Name
      ,T.rows.value('(Person/Names/Last_name)[1]', '[varchar](100)') AS Last_Name
      ,T.rows.value('(Person/@ssn)[1]', '[varchar](100)') AS SSN
      ,T.rows.value('(Person/@e-i)[1]', '[varchar](100)') AS ei
      ,T.rows.value('(Person/Title)[1]', '[varchar](100)') AS Title
      ,T.rows.value('(Person/Position)[1]', '[varchar](100)') AS Position
FROM @xml.nodes('/Records/Record') T(rows)


/* Record to alias, one-to-many mapping */

SELECT T.rows.value('../../../../@category[1]', '[varchar](100)') AS Category
      ,T.rows.value('.', '[varchar](100)') AS Alias
FROM @xml.nodes('/Records/Record/Person/Names/Aliases/Alias') T(rows)

扫码关注云+社区