【问题标题】:How do I return the index of a node from XML stored in an Oracle XML table?如何从存储在 Oracle XML 表中的 XML 返回节点的索引?
【发布时间】:2018-04-19 22:19:28
【问题描述】:

我已将 XML 存储在 Oracle XML 表中,我想查询该表并返回一组四个节点中具有特定属性值的节点的索引值。在下面的示例中,我想返回值为 @correct="True" 的答案的索引。

<answerset set_id="4" >
    <answer correct="True" ><![CDATA[diamond]]></answer>
    <answer correct="False" ><![CDATA[carbon]]></answer>
    <answer correct="False" ><![CDATA[quartz]]></answer>
    <answer correct="False" ><![CDATA[rubinite]]></answer>
</answerset>

我可以很方便地查询表,得到@correct值为“True”的节点:

SELECT item_name, answer
FROM TEST_XML, xmltable('/testitem' passing test_xml.sys_nc_rowinfo$
columns
item_name VARCHAR2(100) path '@item_name',
--distracter VARCHAR2(1000) path 
answer VARCHAR2(1000) path 
'/testitem/answersets/answerset[last()]/answer[@correct="True"]') Item;


ITEM_NAME      ANSWER
MINERAL        DIAMOND

但我似乎无法弄清楚如何让 XPath 函数或其他函数工作以返回答案的索引或位置,在这种情况下,答案是“1”。

这看起来很接近,但我还没有弄清楚如何在 Oracle XML 查询的上下文中使用该功能:

count(answerset[last()]/answer[@correct = 'True']/preceding-sibling::answer)

https://stackoverflow.com/a/16519582/3757890

【问题讨论】:

    标签: xml oracle indexing position


    【解决方案1】:

    您的 XPath 表达式与 XML 文档不匹配。无论如何,我假设您正在寻找FOR ORDINALITY

    SELECT item_name, answer, POSITION
    FROM TEST_XML, 
        xmltable('/testitem' passing test_xml.sys_nc_rowinfo$ columns
        POSITION FOR ORDINALITY,
        item_name VARCHAR2(100) path '@item_name',
        distracter VARCHAR2(1000) path '/testitem/answersets/answerset[last()]/answer[@correct="True"]') Item;
    

    【讨论】:

      猜你喜欢
      • 2011-04-21
      • 2017-08-17
      • 2011-01-31
      • 1970-01-01
      • 1970-01-01
      • 2012-02-04
      • 1970-01-01
      • 1970-01-01
      • 2013-09-06
      相关资源
      最近更新 更多