我在尝试从postgres中的xml字段提取值时遇到问题。我需要确保嵌套元素与它们所属的行保持一致,但似乎并没有这样做。
作为一个例子,我有这个的XML ...
<sample>
<top>
<header><valuea>a</valuea><valueb>b</valueb></header>
<node_a>
<person>
<person_name>John Doe</person_name>
<note>this is a note</note>
<address>
<street>100 Main St</street>
<city>AnyTown</city>
<state>CA</state>
<zip>99999</zip>
<note>Beware of the dog</note>
</address>
<node_b>
<likes>
<item>
<desc>walks on the beach</desc>
<note>only at sunset</note>
</item>
<item>
<desc>spring showers</desc>
<note>bring may flowers</note>
</item>
</likes>
</node_b>
</person>
<person>
<person_name>Jane Doe</person_name>
<note>madame is a true lady</note>
<address>
<street>100 Main St</street>
<city>AnyTown</city>
<state>CA</state>
<zip>99999</zip>
<note>Hell hath no fury</note>
</address>
<node_b>
<likes>
<item>
<desc>wine</desc>
<note>only red</note>
</item>
<item>
<desc>nachos</desc>
<note>by candle light</note>
</item>
</likes>
</node_b>
</person>
</node_a>
</top>
</sample>
sql
SELECT btrim(xpath('//top/header/valuea/text()', xml)::text, '{}') as value_a,
btrim(xpath('//top/header/valueb/text()', xml)::text, '{}') as value_b,
unnest(xpath('//node_a/person/person_name/text()', xml)::text[]) AS person_name,
unnest(xpath('//node_a/person/note/text()', xml)::text[]) AS note,
unnest(xpath('//node_b/likes/item/desc/text()', xml)::text[]) AS likes_desc,
unnest(xpath('//node_b/likes/item/note/text()', xml)::text[]) AS likes_note
FROM
inbox AS t
where repoid=4
产生此结果
简·多伊(Jane Doe)应该在第3行,而不是第2行。
关于如何调整sql以确保notes部分保持正确水平的任何想法吗?