我正在使用xmlelement在oracle中编写一段html代码。
我当前的数据库存储这些数据
+---------+----------------+-----------------------+-----------+ | menu_id | title | url | upper_lvl | +---------+----------------+-----------------------+-----------+ | 1 | Forms | null | 0 | | 2 | Basic forms | forms-basic.html | 1 | | 3 | Extended forms | forms-extended.html | 1 | | 4 | Validation | forms-validation.html | 1 | | 5 | Wizard321 | forms-wizard.html | 1 | +---------+----------------+-----------------------+-----------+
这是我想要的格式:
<ul class="dropdown-menu">
<li>
<a href="forms-basic.html">Basic forms</a>
</li>
<li>
<a href="forms-extended.html">Extended forms</a>
</li>
<li>
<a href="forms-validation.html">Validation</a>
</li>
<li>
<a href="forms-wizard.html">Wizard</a>
</li>
</ul>
我试图使用下面的代码来获取结果,但我知道它不起作用,因为内部查询返回多行。如果有人可以修改或其他解决方案,欢迎来到这里。
select
xmlelement("ul",
xmlattributes('dropdown-menu' as "class"),
xmlelement("li",
xmlelement("a",
xmlattributes(url as "href"),
(select title from att1 where upper_lvl = 1)
)
)
)
from att1
where menu_id = 1;
答案 0 :(得分:1)
您需要使用XMLAgg(XML聚合函数)来实现此目的
select
xmlelement("ul", xmlattributes('dropdown-menu' as "class"),
XMLAgg(xmlelement("li",
xmlelement("a",
xmlattributes(url as "href"),title) as "Menu Element"
)
)
) as Menu
from att1 where upper_lvl = 1
如果这不适合你,请告诉我