5

我正在尝试从 xml 中提取值。当 xml 具有属性时,我遇到了问题。像下面的存储过程

DELIMITER $$
DROP PROCEDURE IF EXISTS `excel`.`insert_items` $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `insert_items`()
BEGIN
SET @xml = '<items><item>
        <value columntype="0">Single Line Text_01</value>
    <value columntype="1">Single Line Text_12341</value>
    <value columntype="2">Single Line Text_21</value>
    <value columntype="3">Single Line Text_31</value>
    <value columntype="4">Single Line Text_41</value>
    </item>
</items>';
SELECT @columntype, ExtractValue(@xml, 'items/item/value[items/item/value/@columntype=0]');    
END $$
DELIMITER ;
4

2 回答 2

10

获取具有属性的元素的值columntype="0"

SELECT ExtractValue(@xml, 'items/item/value[@columntype=0]') value;

输出:

|               VALUE |
-----------------------
| Single Line Text_01 |

SQLFiddle

于 2013-05-10T06:31:41.560 回答
0

以下代码将打印columntypevalue的值:

 DELIMITER $$
    DROP PROCEDURE IF EXISTS `TEST`$$
    CREATE DEFINER=`root`@`localhost` PROCEDURE `TEST`(IN XMLINPUT TEXT)
    BEGIN
    DECLARE VALUECOUNT INT UNSIGNED;
    DECLARE VALUE INT UNSIGNED;
    DECLARE I INT UNSIGNED DEFAULT 1;
    SET VALUECOUNT:=EXTRACTVALUE(XMLINPUT, 'COUNT(/items/item/value)');
    SELECT VALUECOUNT; -- print 5
    WHILE (I <= VALUECOUNT) DO 
        SELECT EXTRACTVALUE(XMLINPUT, '/items/item/value[$I]/@columntype[1]'); -- will print columntype tag
        SELECT EXTRACTVALUE(XMLINPUT, '/items/item/value[$I]'); -- will print value tag     
    SET I:= I+1;
    END WHILE;

    END$$DELIMITER ;
于 2014-12-12T14:18:48.043 回答