如何将XML数据转换为行

时间:2016-01-04 15:35:49

标签: sql xml oracle11g

我有XML:

<PricingItem name="Rate SMS 16116 - ¿¿¿¿¿ (MIoD-USSD) PO 12010" description="" id="##PC4.0##118419" businessEntity="Telco" businessEntityID="##PC4.0##0" pricingItemType="##PC4.0##116343" pricingItemTypeName="FET Occurrence per period and point target and point origin with dynamic SF">
    <Entity>
        <AttributeObjects>
            <AttributeObject xsi:type="AttributeObject" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Item ID" description="" id="##PC4.0##45530" value="118419" />
            <AttributeObject xsi:type="AuxilaryValueAttributeObject" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Charge code" description="" id="##PC4.0##45531" valueName="UC16116" index="395" value="UC16116" />
            <AttributeObject xsi:type="AuxilaryAttributeObject" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Charge codes object" description="" id="##PC4.0##45532" value="Charge codes" />
            <AttributeObject xsi:type="AuxilaryAttributeObject" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Tax change object" description="" id="##PC4.0##45533" value="Tax change" />
            <AttributeObject xsi:type="AuxilaryAttributeObject" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Old age list" description="" id="##PC4.0##45534" value="FET Service filter to old age" />
            <AttributeObject xsi:type="AttributeObject" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Tax percentage" description="" id="##PC4.0##45535" value="0" />
            <AttributeObject xsi:type="AttributeObject" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Outcollect ind" description="" id="##PC4.0##45536" value="N" />
            <AttributeObject xsi:type="AuxilaryAttributeObject" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Period set" description="" id="##PC4.0##45537" value="Single period period set1" />
            <AttributeObject xsi:type="AuxilaryAttributeObject" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Special days set" description="" id="##PC4.0##45538" value="Special day set" />
            <AttributeObject xsi:type="AttributeObject" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Service filter" description="" id="##PC4.0##45539" value="16116" />
            <AttributeObject xsi:type="RatingTableObject" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Rate table" description="" id="##PC4.0##45540" value="Rate table">
                <ComplexCoordinateValues>
                    <ComplexCoordinateValue>
                        <Values>
                            <Value name="Rate" description="" id="##PC4.0##58979" value="3" />
                            <Value name="Quantity" description="" id="##PC4.0##58980" value="1" />
                        </Values>
                        <Coordinates>
                            <Coordinate value="Period N/A" valueID="0" dimensionName="Period" dimensionID="##PC4.0##435" />
                            <Coordinate value="MIoD - NT$3" valueID="29653" dimensionName="Target point" dimensionID="##PC4.0##436" />
                        </Coordinates>
                    </ComplexCoordinateValue>
                    <ComplexCoordinateValue>
                        <Values>
                            <Value name="Rate" description="" id="##PC4.0##58981" value="4" />
                            <Value name="Quantity" description="" id="##PC4.0##58982" value="1" />
                        </Values>
                        <Coordinates>
                            <Coordinate value="Period N/A" valueID="0" dimensionName="Period" dimensionID="##PC4.0##435" />
                            <Coordinate value="MIoD - NT$4" valueID="29654" dimensionName="Target point" dimensionID="##PC4.0##436" />
                        </Coordinates>
                    </ComplexCoordinateValue>
                    <ComplexCoordinateValue>
                        <Values>
                            <Value name="Rate" description="" id="##PC4.0##58983" value="3" />
                            <Value name="Quantity" description="" id="##PC4.0##58984" value="1" />
                        </Values>
                        <Coordinates>
                            <Coordinate value="Period N/A" valueID="0" dimensionName="Period" dimensionID="##PC4.0##435" />
                            <Coordinate value="MIoD - NT$3 - ADE" valueID="29655" dimensionName="Target point" dimensionID="##PC4.0##436" />
                        </Coordinates>
                    </ComplexCoordinateValue>
                    <ComplexCoordinateValue>
                        <Values>
                            <Value name="Rate" description="" id="##PC4.0##58985" value="6" />
                            <Value name="Quantity" description="" id="##PC4.0##58986" value="1" />
                        </Values>
                        <Coordinates>
                            <Coordinate value="Period N/A" valueID="0" dimensionName="Period" dimensionID="##PC4.0##435" />
                            <Coordinate value="MIoD - NT$6" valueID="29656" dimensionName="Target point" dimensionID="##PC4.0##436" />
                        </Coordinates>
                    </ComplexCoordinateValue>
                    <ComplexCoordinateValue>
                        <Values>
                            <Value name="Rate" description="" id="##PC4.0##58987" value="10" />
                            <Value name="Quantity" description="" id="##PC4.0##58988" value="1" />
                        </Values>
                        <Coordinates>
                            <Coordinate value="Period N/A" valueID="0" dimensionName="Period" dimensionID="##PC4.0##435" />
                            <Coordinate value="MIoD - NT$10" valueID="29657" dimensionName="Target point" dimensionID="##PC4.0##436" />
                        </Coordinates>
                    </ComplexCoordinateValue>
                    <ComplexCoordinateValue>
                        <Values>
                            <Value name="Rate" description="" id="##PC4.0##58989" value="15" />
                            <Value name="Quantity" description="" id="##PC4.0##58990" value="1" />
                        </Values>
                        <Coordinates>
                            <Coordinate value="Period N/A" valueID="0" dimensionName="Period" dimensionID="##PC4.0##435" />
                            <Coordinate value="MIoD - NT$15" valueID="29658" dimensionName="Target point" dimensionID="##PC4.0##436" />
                        </Coordinates>
                    </ComplexCoordinateValue>
                    <ComplexCoordinateValue>
                        <Values>
                            <Value name="Rate" description="" id="##PC4.0##58991" value="4" />
                            <Value name="Quantity" description="" id="##PC4.0##58992" value="1" />
                        </Values>
                        <Coordinates>
                            <Coordinate value="Period N/A" valueID="0" dimensionName="Period" dimensionID="##PC4.0##435" />
                            <Coordinate value="MIoD - NT$4 - ADE" valueID="29659" dimensionName="Target point" dimensionID="##PC4.0##436" />
                        </Coordinates>
                    </ComplexCoordinateValue>
                    <ComplexCoordinateValue>
                        <Values>
                            <Value name="Rate" description="" id="##PC4.0##58993" value="6" />
                            <Value name="Quantity" description="" id="##PC4.0##58994" value="1" />
                        </Values>
                        <Coordinates>
                            <Coordinate value="Period N/A" valueID="0" dimensionName="Period" dimensionID="##PC4.0##435" />
                            <Coordinate value="MIoD - NT$6 - ADE" valueID="29660" dimensionName="Target point" dimensionID="##PC4.0##436" />
                        </Coordinates>
                    </ComplexCoordinateValue>
                    <ComplexCoordinateValue>
                        <Values>
                            <Value name="Rate" description="" id="##PC4.0##58995" value="10" />
                            <Value name="Quantity" description="" id="##PC4.0##58996" value="1" />
                        </Values>
                        <Coordinates>
                            <Coordinate value="Period N/A" valueID="0" dimensionName="Period" dimensionID="##PC4.0##435" />
                            <Coordinate value="MIoD - NT$10 - ADE" valueID="29661" dimensionName="Target point" dimensionID="##PC4.0##436" />
                        </Coordinates>
                    </ComplexCoordinateValue>
                    <ComplexCoordinateValue>
                        <Values>
                            <Value name="Rate" description="" id="##PC4.0##58997" value="15" />
                            <Value name="Quantity" description="" id="##PC4.0##58998" value="1" />
                        </Values>
                        <Coordinates>
                            <Coordinate value="Period N/A" valueID="0" dimensionName="Period" dimensionID="##PC4.0##435" />
                            <Coordinate value="MIoD - NT$15 - ADE" valueID="29662" dimensionName="Target point" dimensionID="##PC4.0##436" />
                        </Coordinates>
                    </ComplexCoordinateValue>
                    <ComplexCoordinateValue>
                        <Values>
                            <Value name="Rate" description="" id="##PC4.0##58999" value="5" />
                            <Value name="Quantity" description="" id="##PC4.0##59000" value="1" />
                        </Values>
                        <Coordinates>
                            <Coordinate value="Period N/A" valueID="0" dimensionName="Period" dimensionID="##PC4.0##435" />
                            <Coordinate value="MIoD - NT$5" valueID="29663" dimensionName="Target point" dimensionID="##PC4.0##436" />
                        </Coordinates>
                    </ComplexCoordinateValue>
                    <ComplexCoordinateValue>
                        <Values>
                            <Value name="Rate" description="" id="##PC4.0##59001" value="5" />
                            <Value name="Quantity" description="" id="##PC4.0##59002" value="1" />
                        </Values>
                        <Coordinates>
                            <Coordinate value="Period N/A" valueID="0" dimensionName="Period" dimensionID="##PC4.0##435" />
                            <Coordinate value="MIoD - NT$5 - ADE" valueID="29664" dimensionName="Target point" dimensionID="##PC4.0##436" />
                        </Coordinates>
                    </ComplexCoordinateValue>
                    <ComplexCoordinateValue>
                        <Values>
                            <Value name="Rate" description="" id="##PC4.0##59003" value="1.5" />
                            <Value name="Quantity" description="" id="##PC4.0##59004" value="1" />
                        </Values>
                        <Coordinates>
                            <Coordinate value="Period N/A" valueID="0" dimensionName="Period" dimensionID="##PC4.0##435" />
                            <Coordinate value="SMS - NT$1.5" valueID="29436" dimensionName="Target point" dimensionID="##PC4.0##436" />
                        </Coordinates>
                    </ComplexCoordinateValue>
                    <ComplexCoordinateValue>
                        <Values>
                            <Value name="Rate" description="" id="##PC4.0##59005" value="7.5" />
                            <Value name="Quantity" description="" id="##PC4.0##59006" value="1" />
                        </Values>
                        <Coordinates>
                            <Coordinate value="Period N/A" valueID="0" dimensionName="Period" dimensionID="##PC4.0##435" />
                            <Coordinate value="SMS - NT$7.5" valueID="29437" dimensionName="Target point" dimensionID="##PC4.0##436" />
                        </Coordinates>
                    </ComplexCoordinateValue>
                    <ComplexCoordinateValue>
                        <Values>
                            <Value name="Rate" description="" id="##PC4.0##59007" value="1.1" />
                            <Value name="Quantity" description="" id="##PC4.0##59008" value="1" />
                        </Values>
                        <Coordinates>
                            <Coordinate value="Period N/A" valueID="0" dimensionName="Period" dimensionID="##PC4.0##435" />
                            <Coordinate value="SMS - NT$1.1" valueID="29760" dimensionName="Target point" dimensionID="##PC4.0##436" />
                        </Coordinates>
                    </ComplexCoordinateValue>
                </ComplexCoordinateValues>
            </AttributeObject>
            <AttributeObject xsi:type="AttributeObject" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Point origin" description="" id="##PC4.0##45541" value="12010" />
        </AttributeObjects>
    </Entity>
</PricingItem>

使用的查询:

select  ID,EXTRACT(xmltype(ELEMENT_VALUE), '/PricingItem/Entity/AttributeObjects/AttributeObject/ComplexCoordinateValues/ComplexCoordinateValue/Values/Value/@value').getstringval() as Rate,
 EXTRACT(xmltype(ELEMENT_VALUE), '/PricingItem/Entity/AttributeObjects/AttributeObject/ComplexCoordinateValues/ComplexCoordinateValue/Values/Value/@id') as id,
 EXTRACT(xmltype(ELEMENT_VALUE), '/PricingItem/Entity/AttributeObjects/AttributeObject/ComplexCoordinateValues/ComplexCoordinateValue/Values/Value/@name').getstringval() as Name
  from pc_element where ELEMENT_TYPE='PricingItem' --and ID='165824'
  and EXTRACT(xmltype(ELEMENT_VALUE), '/PricingItem/Entity/AttributeObjects/AttributeObject/@value') is not null

我希望值分成不同的行。 输出如下:

31413161101151416110115151511.517.511.11

这是所有值的串联,但我想要在不同的行中。

1 个答案:

答案 0 :(得分:2)

您可以Xquery使用XMLTable调用将XML节点转换为行:

select x.value, x.id, x.name
from pc_element
cross join xmltable('/PricingItem/Entity/AttributeObjects/AttributeObject/ComplexCoordinateValues/ComplexCoordinateValue/Values/Value'
  passing xmltype(pc_element.element_value)
  columns value number path '@value',
    id varchar2(30) path '@id',
    name varchar2(30) path '@name'
) x
where x.value is not null;

     VALUE ID                             NAME                         
---------- ------------------------------ ------------------------------
         3 ##PC4.0##58979                 Rate                          
         1 ##PC4.0##58980                 Quantity                      
         4 ##PC4.0##58981                 Rate                          
         1 ##PC4.0##58982                 Quantity                      
         3 ##PC4.0##58983                 Rate                          
         1 ##PC4.0##58984                 Quantity                      
...
       1.1 ##PC4.0##59007                 Rate                          
         1 ##PC4.0##59008                 Quantity