I have some XML stored in a settings column in my_table in PostgreSQL. The XML is similar to this:
<Dictionary>
<ExportValues>
<ReplacementSet>
<type>TEST_CODE</type>
<ReplacementPair>
<Input>A1</Input>
<Output>One</Output>
</ReplacementPair>
<ReplacementPair>
<Input>A2</Input>
<Output>Two</Output>
</ReplacementPair>
<ReplacementPair>
<Input>A3</Input>
<Output>Three</Output>
</ReplacementPair>
</ReplacementSet>
<ReplacementSet>
<type>TEST_TYPE</type>
<ReplacementPair>
<Input>MTL</Input>
<Output>Metal</Output>
</ReplacementPair>
<ReplacementPair>
<Input>LQD</Input>
<Output>Liquid</Output>
</ReplacementPair>
<ReplacementPair>
<Input>SLD</Input>
<Output>Solid</Output>
</ReplacementPair>
</ReplacementSet>
</ExportValues>
</Dictionary>
I am trying to get the following output:
type, Input, Output
TEST_CODE, A1, One
TEST_CODE, A2, Two
TEST_CODE, A3, Three
TEST_TYPE, MTL, Metal
TEST_TYPE, LQD, Liquid
TEST_TYPE, SLD, Solid
I am able to get the values from the type node with the following SQL:
select xxx.*
from xmltable('/Dictionary/ExportValues/ReplacementSet'
passing xml((select settings
from my_table
limit 1))
columns replacement_value_type text path 'type') xxx
And I am able to get the values from the Input and Output nodes with the following SQL:
select xxx.*
from xmltable('/Dictionary/ExportValues/ReplacementSet/ReplacementPair'
passing xml((select settings
from web_service
limit 1))
columns our_value text path 'OurValue',
their_value text path 'TheirValue') xxx
However, I can't figure out how to select the value from the respective type nodes with all of the Input and Output node values within the ReplacementSet nodes.
Everything that I have tried has either errored out, included the type values, but nulls for Input and Output, or nulls for the type and values for the Input and Output nodes.