Oracle XMLTABLE 函数。如何合并具有相同名称的节点?
Oracle XMLTABLE Function. How to merge nodes that have the same name?
对 Post 进行了编辑以提供最小的可重现示例
我正在寻找一种将所有名称节点(可能是 2-3-4 等)与它们之间的 space 连接在一起的方法。以下示例给出错误 ORA-19279: XPTY0004 - XQuery dynamic type mismatch: expected singleton sequence - got multi-item sequence
。我知道这是因为有多个名称节点。
我试过更换
"name" VARCHAR2(50) PATH 'name'
和
"name" VARCHAR2(50) PATH 'name[1]'
只会给出第一个值。这消除了错误,但没有将它们连接在一起。
WITH XML_T (XML_FILE_DATA) AS (SELECT '<breakfast_menu>
<food>
<name>Belgian Waffles</name>
<name>Cake</name>
<price>.95</price>
<calories>650</calories>
</food>
<food>
<name>Belgian Waffles(2)</name>
<name>Cake(2)</name>
<price>.95(2)</price>
<calories>650(2)</calories>
</food>
</breakfast_menu>' FROM DUAL)
SELECT x.*
FROM XML_T,
XMLTABLE ('/breakfast_menu/food'
PASSING xmltype (XML_T.XML_FILE_DATA)
COLUMNS "name" VARCHAR2(50) PATH 'name',
"price" VARCHAR2(50) PATH 'price',
"calories" VARCHAR2(50) PATH 'calories') X;
我希望能够获取这 2 个名称并将它们合并到一个列中。期望输出:
+-------------------------+-------------+-------------+
| name | price | calories |
+-------------------------+-------------+-------------+
| Belgian Waffles Cake | .95 | 650 |
| Belgian Waffles Cake(2) | .95(2) | 650(2) |
+-------------------------+-------------+-------------+
@Padders 的回答解决了问题。
您可以像这样在 XPath 选择器中指定要查找的 名称:
WITH xml_t (xml_file_data) AS (SELECT '<breakfast_menu>
<food>
<name>Belgian Waffles</name>
<name>Cake</name>
<price>.95</price>
<calories>650</calories>
</food>
<food>
<name>Belgian Waffles(2)</name>
<name>Cake(2)</name>
<price>.95(2)</price>
<calories>650(2)</calories>
</food>
</breakfast_menu>' FROM DUAL)
SELECT TRIM (name1 || ' ' || name2 || ' ' || name3 || ' ' || name4) AS combined_names, x.*
FROM xml_t,
XMLTABLE ('/breakfast_menu/food'
PASSING xmltype (XML_T.XML_FILE_DATA)
COLUMNS name1 VARCHAR2 (50) PATH 'name[1]',
name2 VARCHAR2 (50) PATH 'name[2]',
name3 VARCHAR2 (50) PATH 'name[3]',
name4 VARCHAR2 (50) PATH 'name[4]',
price VARCHAR2 (50) PATH 'price',
calories VARCHAR2 (50) PATH 'calories') X;
COMBINED_NAMES NAME1 NAME2 NAME3 NAME4 PRICE CALORIES
_____________________________ _____________________ __________ ________ ________ ___________ ___________
Belgian Waffles Cake Belgian Waffles Cake .95 650
Belgian Waffles(2) Cake(2) Belgian Waffles(2) Cake(2) .95(2) 650(2)
您可以在 PATH 字符串中利用 Oracle 对 xquery 表达式的支持,例如
SELECT xt.*
FROM xml_t x,
XMLTABLE (
'breakfast_menu/food'
PASSING x.xml_file_data
COLUMNS
names VARCHAR2 (4000) PATH 'string-join(name," ")',
price VARCHAR2 (50) PATH 'price',
calories VARCHAR2 (50) PATH 'calories') xt;
对 Post 进行了编辑以提供最小的可重现示例
我正在寻找一种将所有名称节点(可能是 2-3-4 等)与它们之间的 space 连接在一起的方法。以下示例给出错误 ORA-19279: XPTY0004 - XQuery dynamic type mismatch: expected singleton sequence - got multi-item sequence
。我知道这是因为有多个名称节点。
我试过更换
"name" VARCHAR2(50) PATH 'name'
和
"name" VARCHAR2(50) PATH 'name[1]'
只会给出第一个值。这消除了错误,但没有将它们连接在一起。
WITH XML_T (XML_FILE_DATA) AS (SELECT '<breakfast_menu>
<food>
<name>Belgian Waffles</name>
<name>Cake</name>
<price>.95</price>
<calories>650</calories>
</food>
<food>
<name>Belgian Waffles(2)</name>
<name>Cake(2)</name>
<price>.95(2)</price>
<calories>650(2)</calories>
</food>
</breakfast_menu>' FROM DUAL)
SELECT x.*
FROM XML_T,
XMLTABLE ('/breakfast_menu/food'
PASSING xmltype (XML_T.XML_FILE_DATA)
COLUMNS "name" VARCHAR2(50) PATH 'name',
"price" VARCHAR2(50) PATH 'price',
"calories" VARCHAR2(50) PATH 'calories') X;
我希望能够获取这 2 个名称并将它们合并到一个列中。期望输出:
+-------------------------+-------------+-------------+
| name | price | calories |
+-------------------------+-------------+-------------+
| Belgian Waffles Cake | .95 | 650 |
| Belgian Waffles Cake(2) | .95(2) | 650(2) |
+-------------------------+-------------+-------------+
@Padders 的回答解决了问题。
您可以像这样在 XPath 选择器中指定要查找的 名称:
WITH xml_t (xml_file_data) AS (SELECT '<breakfast_menu>
<food>
<name>Belgian Waffles</name>
<name>Cake</name>
<price>.95</price>
<calories>650</calories>
</food>
<food>
<name>Belgian Waffles(2)</name>
<name>Cake(2)</name>
<price>.95(2)</price>
<calories>650(2)</calories>
</food>
</breakfast_menu>' FROM DUAL)
SELECT TRIM (name1 || ' ' || name2 || ' ' || name3 || ' ' || name4) AS combined_names, x.*
FROM xml_t,
XMLTABLE ('/breakfast_menu/food'
PASSING xmltype (XML_T.XML_FILE_DATA)
COLUMNS name1 VARCHAR2 (50) PATH 'name[1]',
name2 VARCHAR2 (50) PATH 'name[2]',
name3 VARCHAR2 (50) PATH 'name[3]',
name4 VARCHAR2 (50) PATH 'name[4]',
price VARCHAR2 (50) PATH 'price',
calories VARCHAR2 (50) PATH 'calories') X;
COMBINED_NAMES NAME1 NAME2 NAME3 NAME4 PRICE CALORIES
_____________________________ _____________________ __________ ________ ________ ___________ ___________
Belgian Waffles Cake Belgian Waffles Cake .95 650
Belgian Waffles(2) Cake(2) Belgian Waffles(2) Cake(2) .95(2) 650(2)
您可以在 PATH 字符串中利用 Oracle 对 xquery 表达式的支持,例如
SELECT xt.*
FROM xml_t x,
XMLTABLE (
'breakfast_menu/food'
PASSING x.xml_file_data
COLUMNS
names VARCHAR2 (4000) PATH 'string-join(name," ")',
price VARCHAR2 (50) PATH 'price',
calories VARCHAR2 (50) PATH 'calories') xt;