0

I have a problem with writing a function in postgresql that gets input with xml data, then gets all nodes and for nodes that ends with work 'key' calls a function. In example i have to do:

function readdata(data xml) RETURNS VOID AS $$
INSERT INTO data_table SELECT FROM xpath('//text()',data)

Some of xml is like:

< node1>sometext< /node1>
< node2_key>anyvalue< /node2_key>
< node3_key>integer< /node3_key>
< node4>anyvalue< /node4>

But i dont know how to call function like check_val('node2',valueof node2_key ) when selecting from data parameter.

What im trying to achive is: that in my SELECT in myfunction it gives me value of nodes when they don't end up with word 'key',and for nodes that ends with it gives me result of check_val function.

Is there a way to do it with postgresql 9.1? I would be grateful with any example of code.

1 Answer 1

1

I don't know about Postgres, but the XPath to get the text of all the nodes (elements) whose name ends with key is:

//*[string-length(name())>2 and substring(name(),string-length(name())-2,3)='key']/text()

and the XPath to get the text of all the nodes (elements) whose names do NOT end with key is:

//*[string-length(name())<3 or substring(name(),string-length(name())-2,3)!='key']/text()
Sign up to request clarification or add additional context in comments.

1 Comment

thats not what i meant exacly, i want to have in output something like SELECT node1_value,check_val('node2',valueof node2_key ),check_val('node3',valueof node3_key ), node4_value

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.