1

Oracle supports dynamic XMLElement name with evalname function. Is there a similar feature in postgres to get the XMLElement name dynamically instead of using constant?

Example in ORACLE:

select xmlelement(evalname(ENAME),EMPNO) from EMP;

This statement will result in list of enames as separate xml elements.

<SMITH>7369</SMITH>
<ALLEN>7499</ALLEN>
<WARD>7521</WARD>

Not sure if postgres has something similar.

Thanks.

4
  • 2
    It would help to provide an example with the expected outcome. Commented Mar 16, 2015 at 22:39
  • Please edit the question to improve it. Never hide essential information in comments. Click on "edit" left under your question. And add the original XML value to make the example more useful. Commented Mar 17, 2015 at 14:40
  • Why did you revert the improvements by a_horse? Please show example values. This might be of help: stackoverflow.com/questions/7491479/… Commented Mar 17, 2015 at 15:17
  • See also XML element name from data in Postgres Commented Oct 10, 2020 at 23:22

2 Answers 2

3

I was able to get a workaround to construct xml with dynamic element names in Postgres using execute format. Posting this just in case if anyone had same issue.

execute format('SELECT XMLElement(NAME %I, $1)', emp_name) USING empno from emp;

<SMITH>7369</SMITH>
<ALLEN>7499</ALLEN>

Same worked with XMLForest and having XMLAttributes inside XMLElement.

Sign up to request clarification or add additional context in comments.

Comments

0

There is no such function as far as I know.

The closest you can get is adding an attribute with the empname:

select xmlelement(name emp, xmlattributes(empname), empno)
from emp;

Generates:

<emp empname="Smith">7369</emp>
<emp empname="Allend">7499</emp>
<emp empname="Ward">7521</emp>

Personally I would find that format much easier to parse e.g. in XSLT or an XML parser. Because in order to process a tag you would need to know the tag name, which you don't if the tag changes for each row - but this might just be me.

3 Comments

Thanks for inputs. This doesn't help for me coz I am trying to migrate existing function from oracle and was trying avoid changing the xml schema. Is there any way to get dynamic values for names either by selecting from table or calling function which returns list of columns instead of having a constant value for XMLElement name?
@PrasadChinni: apart from writing your own function for that, no I don't think so.
I was trying to write a custom function. Is there any way to invoke the function for XMLElement name? Thanks.

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.