1

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.

1 Answer 1

2

It is not a problem, but you have to specify XPath for "type" column explicitly:

select x.* 
  from my_table, 
       xmltable('/Dictionary/ExportValues/ReplacementSet/ReplacementPair'
                passing settings 
                columns
                  type text path '../type', 
                  input text path 'Input', 
                  output text path 'Output') x;

+-----------+-------+--------+
|   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  |
+-----------+-------+--------+
(6 rows)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.