我有一个表,其中一列作为XMLTYPE与Object-Relational存储一起存储。下面是表格ddl。
CREATE TABLE Orders ( Order_id number not null,
Order_status Varchar2(100),
Order_desc XMLType not null)
XMLTYPE Order_desc STORE AS OBJECT RELATIONAL
XMLSCHEMA "http://localhost/public/xsd/Orderstore.xsd"
ELEMENT "OrderVal"
);
我已成功注册架构以使用XML DB加载XSD。下面是加载到XMLTYPE列中的XML。
<?xml version="1.0" encoding="utf-8" ?>
<draftorders>
<OrderSumm>
<Ordercod>OrderBookings</Ordercod>
</OrderSumm>
<Orderattrs>
<Orderattr Ordername="HROrder">
<OrderVals>
<OrderVal>
<listvalue>Order1</listvalue>
<Orderattrs>
<Orderattr Ordername="Node1_Child1">
<OrderVals>
<OrderVal>
<listvalue><![CDATA[ Node1_Child1_OrderValue_1]]></listvalue>
<Orderattrs>
<Orderattr Ordername="Node2_Child1">
<OrderVals>
<OrderVal>
<listvalue><![CDATA[ Node2_Child1_OrderValue_1]]></listvalue>
</OrderVal>
</OrderVals>
</Orderattr>
<Orderattr Ordername="Node2_Child2">
<OrderVals>
<OrderVal>
<listvalue><![CDATA[ Node2_Child2_OrderValue_1]]></listvalue>
</OrderVal>
</OrderVals>
</Orderattr>
</Orderattrs>
</OrderVal>
</OrderVals>
</Orderattr>
</Orderattrs>
</OrderVal>
</OrderVals>
</Orderattr>
</Orderattrs>
</draftorders>
我使用“extract”查询打印以下输出:
SELECT extract(o.Order_desc,'/OrderVal[1]/Orderattrs/Orderattr[1]/OrderVals/OrderVal[1]/Orderattrs/Orderattr[0]/@Ordername').getStringVal() "Node1",
extract(o.Order_desc,'/OrderVal[1]/Orderattrs/Orderattr[1]/OrderVals/OrderVal[1]/Orderattrs/Orderattr[0]/OrderVals/OrderVal[1]/listvalue/text()').getStringVal() "Node1Child",
extract(o.Order_desc,'/OrderVal[1]/Orderattrs/Orderattr[1]/OrderVals/OrderVal[1]/Orderattrs/Orderattr[1]/@Ordername').getStringVal() "Node2",
extract(c.Order_desc,'/OrderVal[1]/Orderattrs/Orderattr[1]/OrderVals/OrderVal[1]/Orderattrs/Orderattr[1]/OrderVals/OrderVal[1]/listvalue/text()').getStringVal() "Node2Child"
FROM Orders o;
OUTPUT:-
Node2_Child1
Node2_Child1_OrderValue_1
Node2_Child2
Node2_Child2_OrderValue_1
我想使用XMLQuery实现相同的输出,但我无法构建查询来打印子节点。到目前为止,我只能使用XMLQuery打印节点值,如下所示: -
SELECT XMLQuery( '/OrderVal[1]/Orderattrs/Orderattr[1]/OrderVals/OrderVal[1]/Orderattrs/Orderattr[0]/@Ordername'
PASSING o.Order_desc RETURNING CONTENT
)
FROM Orders o;
如何使用“extract”和“XMLQuery”来实现相同的输出?
感谢。
/ * ** * **** 修改后的查询运行: -
SELECT XMLQuery('//OrderVal/Orderattrs/Orderattr/(@Ordername, OrderVals/OrderVal/listvalue)/data(.)'
PASSING o.Order_desc RETURNING CONTENT
)
FROM Orders o;
输出: -
Node2_Child1
Node2_Child1_OrderValue_1
Node2_Child
使用XMLTABLE获取所有节点及其子节点。
SELECT ord.OrdName, ord.OrdVal
FROM Orders, XMLTable('/OrderVal[1]/Orderattrs/Orderattr[1]/OrderVals/OrderVal[1]/Orderattrs/Orderattr'
PASSING Order_desc
COLUMNS "OrdName" VARCHAR2(4000) PATH '@Ordername',
"OrdVal" VARCHAR2(4000) PATH 'OrderVals/OrderVal[1]/listvalue') ord;
输出: -
Node2_Child1
Node2_Child1_OrderValue_1
Node2_Child2
Node2_Child2_OrderValue_1
......
Node2_Child2500
Node2_Child2500_OrderValue_1
如何使用XMLQuery实现相同的功能?
答案 0 :(得分:0)
您的查询和示例XML似乎不匹配,因此我尽可能与您的查询保持一致。由于您的XML被破坏,您可能还需要调整一些轴步骤。
您可以使用括号在某个时刻“分支”(例如,获取属性值和子节点)。对于任意数量的订单属性,此查询将立即获取所有结果:
//OrderVal/Orderattrs/Orderattr/(@Ordername, OrderVals/OrderVal/listvalue)/data(.)