获取XML数据中的XML值

huangapple go评论65阅读模式
英文:

Get XML value from XML data

问题

I want to get only some part of XML value.

Given sample data:

<Root>
  <row id="1"><name>Sam</name><address>dummy address</address></row>
  <row id="2"><name>Mak</name></row>
  <row id="3" />
</Root>

I want to return only name values Sam,Mak.

My try:

with cte as 
(
  select '<Root>
            <row id="1"><name>Sam</name><address>dummy address</address></row>
            <row id="2"><name>Mak</name></row>
            <row id="3" />
          </Root>'::xml as xm
) 
select unnest(xpath('//Root/row/name/text()', xm))
from cte;

Expected output:

    unnest
    -----------------
    Sam
    Mak
英文:

I want to get only some part of XML value.

Given sample data:

<!-- language: XML -->

&lt;Root&gt;  
  &lt;row id=&quot;1&quot;&gt;&lt;name&gt;Sam&lt;/name&gt;&lt;address&gt;dummy address&lt;/address&gt;&lt;/row&gt;  
  &lt;row id=&quot;2&quot;&gt;&lt;name&gt;Mak&lt;/name&gt;&lt;/row&gt;  
  &lt;row id=&quot;3&quot; /&gt;  
&lt;/Root&gt;

I want to return only name values Sam,Mak.

My try:

with cte as 
(
  select &#39;&lt;Root&gt;  
			&lt;row id=&quot;1&quot;&gt;&lt;name&gt;Sam&lt;/name&gt;&lt;address&gt;dummy address&lt;/address&gt;&lt;/row&gt;  
			&lt;row id=&quot;2&quot;&gt;&lt;name&gt;Mak&lt;/name&gt;&lt;/row&gt;  
			&lt;row id=&quot;3&quot; /&gt;  
		  &lt;/Root&gt;&#39;::xml as xm
) 
select unnest(xpath(&#39;//Root/row/name&#39;, xm))
from cte;

Output:

unnest
-----------------
&lt;name&gt;Sam&lt;/name&gt;
&lt;name&gt;Mak&lt;/name&gt;

Expected output:

unnest
-----------------
Sam
Mak

答案1

得分: 1

这通常更容易使用 xmltable() 函数来实现:

with cte as 
(
  select '&#39;&lt;Root&gt;  
            &lt;row id=&quot;1&quot;&gt;&lt;name&gt;Sam&lt;/name&gt;&lt;address&gt;dummy address&lt;/address&gt;&lt;/row&gt;  
            &lt;row id=&quot;2&quot;&gt;&lt;name&gt;Mak&lt;/name&gt;&lt;/row&gt;  
            &lt;row id=&quot;3&quot; /&gt;  
          &lt;/Root&gt;'::xml as xm
) 
select p.*
from cte
  cross join xmltable('/Root/row' passing xm
                      columns id    text path '@id',
                              name  text path 'name') as p;

结果:

id | name
---+-----
1  | Sam 
2  | Mak 
3  |
英文:

This is typically easier with xmltable()

with cte as 
(
  select &#39;&lt;Root&gt;  
            &lt;row id=&quot;1&quot;&gt;&lt;name&gt;Sam&lt;/name&gt;&lt;address&gt;dummy address&lt;/address&gt;&lt;/row&gt;  
            &lt;row id=&quot;2&quot;&gt;&lt;name&gt;Mak&lt;/name&gt;&lt;/row&gt;  
            &lt;row id=&quot;3&quot; /&gt;  
          &lt;/Root&gt;&#39;::xml as xm
) 
select p.*
from cte
  cross join xmltable(&#39;/Root/row&#39; passing xm
                      columns id    text path &#39;@id&#39;,
                              name  text path &#39;name&#39;) as p;

Returns:

id | name
---+-----
1  | Sam 
2  | Mak 
3  |     

huangapple
  • 本文由 发表于 2023年2月23日 20:58:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75545157.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定