在这个语句中,它为Food_Code返回null,我不太清楚为什么。我对此并不十分了解,任何帮助都将不胜感激
DECLARE
x XMLType := XMLType(
'<?xml version="1.0" ?>
<Food_Display_Row>
<Food_Code>12350000</Food_Code> <Display_Name>Sour cream dip</Display_Name> <Portion_Default>1.00000</Portion_Default> <Portion_Amount>.25000</Portion_Amount>
<Portion_Display_Name>cup</Portion_Display_Name> <Factor>.25000</Factor> <Incrementx>.25000</Incrementx> <Multiplier>1.00000</Multiplier> <Grains>.04799</Grains>
<Whole_Grains>.00000</Whole_Grains> <Vegetables>.04070</Vegetables> <Orange_Vegetables>.00000</Orange_Vegetables> <Drkgreen_Vegetables>.00000</Drkgreen_Vegetables>
<Starchy_vegetables>.00000</Starchy_vegetables> <Other_Vegetables>.04070</Other_Vegetables> <Fruits>.00000</Fruits> <Milk>.00000</Milk> <Meats>.00000</Meats>
<Soy>.00000</Soy> <Drybeans_Peas>.00000</Drybeans_Peas> <Oils>.00000</Oils> <Solid_Fats>105.64850</Solid_Fats> <Added_Sugars>1.57001</Added_Sugars>
<Alcohol>.00000</Alcohol> <Calories>133.65000</Calories> <Saturated_Fats>7.36898</Saturated_Fats>
</Food_Display_Row>');
BEGIN
FOR r IN (
SELECT ExtractValue(Value(p),'/Food_Code/text()') as Food_Code
,ExtractValue(Value(p),'/Display_Name/text()') as Display_Name
,ExtractValue(Value(p),'/Portion_Default/text()') as Portion_Default
,ExtractValue(Value(p),'/Portion_Amount/text()') as Portion_Amount
,ExtractValue(Value(p),'/Portion_Display_Name/text()') as Portion_Display_Name
,ExtractValue(Value(p),'/Factor/text()') as Factor
,ExtractValue(Value(p),'/Incrementx/text()') as Incrementx
,ExtractValue(Value(p),'/Multiplier/text()') as Multiplier
,ExtractValue(Value(p),'/Grains/text()') as Grains
,ExtractValue(Value(p),'/Whole_Grains/text()') as Whole_Grains
,ExtractValue(Value(p),'/Vegetables/text()') as Vegetables
,ExtractValue(Value(p),'/Orange_Vegetables/text()') as Orange_Vegetables
,ExtractValue(Value(p),'/Drkgreen_Vegetables/text()') as Drkgreen_Vegetables
,ExtractValue(Value(p),'/Starchy_vegetables/text()') as Starchy_vegetables
,ExtractValue(Value(p),'/Other_Vegetables/text()') as Other_Vegetables
,ExtractValue(Value(p),'/Fruits/text()') as Fruits
,ExtractValue(Value(p),'/Milk/text()') as Milk
,ExtractValue(Value(p),'/Meats/text()') as Meats
,ExtractValue(Value(p),'/Soy/text()') as Soy
,ExtractValue(Value(p),'/Drybeans_Peas/text()') as Drybeans_Peas
,ExtractValue(Value(p),'/Oils/text()') as Oils
,ExtractValue(Value(p),'/Solid_Fats/text()') as Solid_Fats
,ExtractValue(Value(p),'/Added_Sugars/text()') as Added_Sugars
,ExtractValue(Value(p),'/Alcohol/text()') as Alcohol
,ExtractValue(Value(p),'/Calories/text()') as Calories
,ExtractValue(Value(p),'/Saturated_Fats/text()') as Saturated_Fats
FROM TABLE(XMLSequence(Extract(x,'/Food_Display_Row'))) p
) LOOP
insert into Food_Display_Table values (r.Food_Code, r.Display_Name, r.Portion_Default, r.Portion_Amount, r.Portion_Display_Name,
r.Factor, r.Incrementx, r.Multiplier, r.Grains, r.Whole_Grains, r.Vegetables, r.Orange_Vegetables, r.Drkgreen_Vegetables, r.Starchy_vegetables,
r.Other_Vegetables, r.Fruits, r.Milk, r.Meats, r.Soy, r.Drybeans_Peas, r.Oils, r.Solid_Fats, r.Added_Sugars, r.Alcohol, r.Calories, r.Saturated_Fats);
END LOOP;
END;
我认为问题在于ExtractValue中的/ text(),但我不知道为什么它会返回一个空值而不是给我一些错误。
答案 0 :(得分:3)
Extract函数只删除XML标头但保留在行节点上。在ExtractValue中使用* / XMLPath:
SQL> SELECT ExtractValue(Value(p),'*/Food_Code/text()') as Food_Code
2 ,ExtractValue(Value(p),'*/Display_Name/text()') as Display_Name
3 FROM TABLE(XMLSequence(Extract(XMLType(
4 '<?xml version="1.0" ?><Food_Display_Row>
5 <Food_Code>12350000</Food_Code> <Display_Name>Sour cream dip</Display_Name> <Portion_Default>1.00000</Portion_Default> <Portion_Amount>.25000</Portion_Amount>
6 <Portion_Display_Name>cup</Portion_Display_Name> <Factor>.25000</Factor> <Incrementx>.25000</Incrementx> <Multiplier>1.00000</Multiplier> <Grains>.04799</Grains>
7 <Whole_Grains>.00000</Whole_Grains> <Vegetables>.04070</Vegetables> <Orange_Vegetables>.00000</Orange_Vegetables> <Drkgreen_Vegetables>.00000</Drkgreen_Vegetables>
8 <Starchy_vegetables>.00000</Starchy_vegetables> <Other_Vegetables>.04070</Other_Vegetables> <Fruits>.00000</Fruits> <Milk>.00000</Milk> <Meats>.00000</Meats>
9 <Soy>.00000</Soy> <Drybeans_Peas>.00000</Drybeans_Peas> <Oils>.00000</Oils> <Solid_Fats>105.64850</Solid_Fats> <Added_Sugars>1.57001</Added_Sugars>
10 <Alcohol>.00000</Alcohol> <Calories>133.65000</Calories> <Saturated_Fats>7.36898</Saturated_Fats>
11 </Food_Display_Row>'),'/Food_Display_Row'))) p
12 /
FOOD_CODE DISPLAY_NAME
-------------------- ----------------------------------------
12350000 Sour cream dip