postgres xpath所有节点并使用少数几个函数

时间:2021-02-21 22:17:28

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:

我有一个问题,在postgresql中编写一个函数,用xml数据输入,然后获取所有节点,以及以工作'key'结束的节点调用一个函数。在示例中,我必须这样做:

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

Some of xml is like:

一些xml就像:

< 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.

但是,当从数据参数中选择时,我不知道如何调用check_val('node2',valueof node2_key)之类的函数。

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.

我试图实现的是:在我的SELECT函数中,当它们没有以“key”结尾时,它给出了节点的值,对于以它结尾的节点,它给出了check_val函数的结果。

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

有没有办法用postgresql 9.1做到这一点?我会对任何代码示例表示感谢。

1 个解决方案

#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:

我不知道Postgres,但是获取名称以key结尾的所有节点(元素)的文本的XPath是:

//*[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:

以及获取名称不以key结尾的所有节点(元素)的文本的XPath是:

//*[string-length(name())<3 or substring(name(),string-length(name())-2,3)!='key']/text()

#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:

我不知道Postgres,但是获取名称以key结尾的所有节点(元素)的文本的XPath是:

//*[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:

以及获取名称不以key结尾的所有节点(元素)的文本的XPath是:

//*[string-length(name())<3 or substring(name(),string-length(name())-2,3)!='key']/text()