How to return data in format:
Column Name t01 t02 t03 t04
Data c01 c02 c03 c04
<orders xmlns="www address">
<order>
<order-date>2019-09-05</order-date>
<created-by>storefront</created-by>
<original-order-no>000001</original-order-no>
<currency>USD</currency>
<taxation>gross</taxation>
<invoice-no>0099999</invoice-no>
<custom-attributes>
<custom-attribute attribute-id="t01">c01</custom-attribute>
<custom-attribute attribute-id="t02">c02</custom-attribute>
<custom-attribute attribute-id="t03">c03</custom-attribute>
<custom-attribute attribute-id="t04">c04</custom-attribute>
</custom-attributes>
</order>
</orders>
From your question there's one thing not clear: The naming of the output columns.
In your expected output they are named like their
attribute-id
. But in your comments it sounds, like you are picking the first 4 attributes and you want to omit the rest.I want to show two approaches, pick the one you like more:
--This query will use the
attribute-id
to pick the corresponding attribute.--We can savely return this with the same name
--If your XML does not have the corresponding attribute, there will be a NULL value
--This one is easier. It will pick just the first four attributes, no matter what id they have.