1

我正在从表中提取一个 XML 字段。我正在从中解析数据,并且最初我没有问题提取标准元素信息。我的问题是这个。例如,如果我遇到一个 XML 字符串并且它具有未知数量的“Notes”元素怎么办?它可能有 1、2、3、10、30 或任何数量的“注释”元素。下面是一个例子:

<Person>
    <PersonID>
     123456 
        <PersonName>
        John Doe 
            <Note>
                <PersonID>9876</PersonID>
                <NoteType>C</NoteType>
                <NoteCount>1</NoteCount>
                <Note>AX</Note>
            </Note>
            <Note>
                <PersonID>534354345</PersonID>
                <NoteType>C</NoteType>
                <NoteCount>2</NoteCount>
                <NoteDate>1994-05-17T00:00:00-04:00</NoteDate>
                <Note>Note #2</Note>
            </Note>
            <Note>
                <PersonID>649349873498</PersonID>
                <NoteType>C</NoteType>
                <NoteCount>3</NoteCount>
                <NoteDate>1994-06-24T00:00:00-04:00</NoteDate>
                <Note>More notes are in here.</Note>
            </Note>
            <Note>
                <PersonID>432834987430987AAAAAA</PersonID>
                <NoteType>C</NoteType>
                <NoteCount>4</NoteCount>
                <NoteDate>1994-06-29T00:00:00-04:00</NoteDate>
                <Note>And this is the last note element. </Note>
            </Note>
        </PersonName>
    </PersonID>
</Person>

t-sql 中有没有办法解析出未知数量的元素?如果是这样,怎么做?

谢谢!!

4

1 回答 1

1

将元素嵌套在当前拥有的<PersonName>元素中没有多大意义。<PersonId>为了解析您的 XML,让单个元素包含文本或子元素而不是两者都更容易。我会推荐这样的东西:

<Person>
    <PersonID>123456</PersonID>
    <PersonName>John Doe</PersonName>
    <Note>
        <PersonID>9876</PersonID>
        <NoteType>C</NoteType>
        <NoteCount>1</NoteCount>
        <NoteMessage>AX</NoteMessage>
    </Note>
    <Note>
        <PersonID>534354345</PersonID>
        <NoteType>C</NoteType>
        <NoteCount>2</NoteCount>
        <NoteDate>1994-05-17T00:00:00-04:00</NoteDate>
        <NoteMessage>Note #2</NoteMessage>
    </Note>
    <Note>
        <PersonID>649349873498</PersonID>
        <NoteType>C</NoteType>
        <NoteCount>3</NoteCount>
        <NoteDate>1994-06-24T00:00:00-04:00</NoteDate>
        <NoteMessage>More notes are in here.</NoteMessage>
    </Note>
    <Note>
        <PersonID>432834987430987AAAAAA</PersonID>
        <NoteType>C</NoteType>
        <NoteCount>4</NoteCount>
        <NoteDate>1994-06-29T00:00:00-04:00</NoteDate>
        <NoteMessage>And this is the last note element.</NoteMessage>
    </Note> 
</Person>

为了减少歧义问题,我将内部<Note>元素重命名为<NoteMessage>. 一旦您的 XML 采用与此类似的形式,您就可以使用 XPATH 来遍历您的 XML:

DECLARE @xml xml
SET @xml = 
'<Person>
    <PersonID>123456</PersonID>
    <PersonName>John Doe</PersonName>
    <Note>
        <PersonID>9876</PersonID>
        <NoteType>C</NoteType>
        <NoteCount>1</NoteCount>
        <NoteMessage>AX</NoteMessage>
    </Note>
    <Note>
        <PersonID>534354345</PersonID>
        <NoteType>C</NoteType>
        <NoteCount>2</NoteCount>
        <NoteDate>1994-05-17T00:00:00-04:00</NoteDate>
        <NoteMessage>Note #2</NoteMessage>
    </Note>
    <Note>
        <PersonID>649349873498</PersonID>
        <NoteType>C</NoteType>
        <NoteCount>3</NoteCount>
        <NoteDate>1994-06-24T00:00:00-04:00</NoteDate>
        <NoteMessage>More notes are in here.</NoteMessage>
    </Note>
    <Note>
        <PersonID>432834987430987AAAAAA</PersonID>
        <NoteType>C</NoteType>
        <NoteCount>4</NoteCount>
        <NoteDate>1994-06-29T00:00:00-04:00</NoteDate>
        <NoteMessage>And this is the last note element.</NoteMessage>
    </Note> 
</Person>'

SELECT
    n.value('(./PersonID/text())[1]', 'Varchar(50)') as PersonID,
    n.value('(./NoteType/text())[1]', 'char(1)') as NoteType,
    n.value('(./NoteCount/text())[1]', 'int') as NoteCount,
    n.value('(./NoteDate/text())[1]', 'datetime') as NoteDate,
    n.value('(./NoteMessage/text())[1]', 'Varchar(50)') as NoteMessage
FROM @xml.nodes('/Person/Note') as a(n)
于 2013-11-05T15:17:25.293 回答