oracle 11g中的Xml

时间:2016-09-28 16:16:39

标签: xml oracle

美好的一天。

我在“xmltype”类型的变量中有一个xml。 有问题的xml是这样的:

<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
<soap-env:Header />
<soap-env:Body>
    <n0:EmployeeDataByIdentificationResponse_sync xmlns:n0="http://sap.com/xi/SAPGlobal20/Global" xmlns:prx="urn:sap.com:proxy:GH6:/1SAI/TASBA84FA60747163FD5188:804">
        <EmployeeData>
            <ChangeStateID>                 20160923221906.7365850</ChangeStateID>
            <UUID>00163e06-cc41-1ee3-bb8f-eebd7b8a466e</UUID>
            <EmployeeID>300056</EmployeeID>
            <EmploymentData>
                <UUID>00163e06-cc41-1ed3-bb9a-250ba8cddcd3</UUID>
                <ChangeStateID>                 20140605164803.5438320</ChangeStateID>
                <EmploymentCountryCode>MX</EmploymentCountryCode>
                <WorkAgreementData>
                    <UUID>00163e06-cc41-1ed3-bb9a-250ba8ce5cd3</UUID>
                    <ChangeStateID>                 20160730162955.5720390</ChangeStateID>
                    <WorkAgreementID>300056/001</WorkAgreementID>
                    <ValidityPeriod>
                        <StartDate>1998-05-18</StartDate>
                        <EndDate>9999-12-31</EndDate>
                    </ValidityPeriod>
                    <AdditionalClauses>
                        <ValidityPeriod>
                            <StartDate>1998-05-18</StartDate>
                            <EndDate>9999-12-31</EndDate>
                        </ValidityPeriod>
                        <AgreedWorkingTimeRate>
                            <DecimalValue>8.0</DecimalValue>
                            <BaseMeasureUnitCode>DAY</BaseMeasureUnitCode>
                        </AgreedWorkingTimeRate>
                        <WorkAgreementTypeCode>1</WorkAgreementTypeCode>
                        <WorkAgreementAdministrativeCategoryCode listID="MX">2</WorkAgreementAdministrativeCategoryCode>
                    </AdditionalClauses>
                    <OrganisationalAssignment>
                        <ValidityPeriod>
                            <StartDate>2013-01-01</StartDate>
                            <EndDate>9999-12-31</EndDate>
                        </ValidityPeriod>
                        <PositionAssignment>
                            <ValidityPeriod>
                                <StartDate>2013-01-01</StartDate>
                                <EndDate>9999-12-31</EndDate>
                            </ValidityPeriod>
                            <PositionUUID>00163e06-cc41-1ed3-bb9a-24e569a75cd3</PositionUUID>
                            <PositionID>POS30005620140605164</PositionID>
                            <AssignmentPercent>100.0</AssignmentPercent>
                            <OrganisationalCenterDetails>
                                <ValidityPeriod>
                                    <StartDate>2013-01-01</StartDate>
                                    <EndDate>9999-12-31</EndDate>
                                </ValidityPeriod>
                                <OrganisationalCenterUUID>00163e06-cc41-1ed3-bb82-fea27ecddec6</OrganisationalCenterUUID>
                                <OrganisationalCenterID>LFSA11514</OrganisationalCenterID>
                            </OrganisationalCenterDetails>
                            <JobAssignmentDetails>
                                <ValidityPeriod>
                                    <StartDate>2013-01-01</StartDate>
                                    <EndDate>9999-12-31</EndDate>
                                </ValidityPeriod>
                                <JobUUID>00163e06-cc41-1ee3-bb88-68132a6b7aa2</JobUUID>
                                <JobID>PR006</JobID>
                            </JobAssignmentDetails>
                        </PositionAssignment>
                        <ReportingLineUnit>
                            <Id>LFSA11000</Id>
                            <Name>Laboratorios Finlay San Pedro Sula</Name>
                            <ValidityPeriod>
                                <StartDate>2013-01-01</StartDate>
                                <EndDate>2013-12-31</EndDate>
                            </ValidityPeriod>
                        </ReportingLineUnit>
                        <ReportingLineUnit>
                            <Id>LFSA11510</Id>
                            <Name>Produccion</Name>
                            <ValidityPeriod>
                                <StartDate>2014-01-01</StartDate>
                                <EndDate>2014-12-14</EndDate>
                            </ValidityPeriod>
                        </ReportingLineUnit>
                        <ReportingLineUnit>
                            <Id>LFSA11510</Id>
                            <Name>Produccion</Name>
                            <ValidityPeriod>
                                <StartDate>2014-12-15</StartDate>
                                <EndDate>9999-12-31</EndDate>
                            </ValidityPeriod>
                        </ReportingLineUnit>
                        <CostCenter>
                            <Id>LFSA11514</Id>
                            <Name>Liquidos Jarabes</Name>
                            <ValidityPeriod>
                                <StartDate>2013-01-01</StartDate>
                                <EndDate>2013-12-31</EndDate>
                            </ValidityPeriod>
                        </CostCenter>
                        <CostCenter>
                            <Id>LFSA11514</Id>
                            <Name>Liquidos Jarabes</Name>
                            <ValidityPeriod>
                                <StartDate>2014-01-01</StartDate>
                                <EndDate>2014-12-14</EndDate>
                            </ValidityPeriod>
                        </CostCenter>
                        <CostCenter>
                            <Id>LFSA11514</Id>
                            <Name>Liquidos Jarabes</Name>
                            <ValidityPeriod>
                                <StartDate>2014-12-15</StartDate>
                                <EndDate>9999-12-31</EndDate>
                            </ValidityPeriod>
                        </CostCenter>
                    </OrganisationalAssignment>
                </WorkAgreementData>
            </EmploymentData>
            <BiographicalData>
                <ValidityPeriod>
                    <StartDate>0001-01-01</StartDate>
                    <EndDate>9999-12-31</EndDate>
                </ValidityPeriod>
                <FormOfAddressCode>0001</FormOfAddressCode>
                <AcademicTitleCode>Z011</AcademicTitleCode>
                <GivenName>Maria</GivenName>
                <FamilyName>Reyes</FamilyName>
                <AdditionalFamilyName>Membreno</AdditionalFamilyName>
                <MiddleName>Elena</MiddleName>
                <GenderCode>2</GenderCode>
                <BirthName>ronal</BirthName>
                <BirthDate>1967-11-20</BirthDate>
                <MaritalStatusCode>1</MaritalStatusCode>
                <NationalityCountryCode>HN</NationalityCountryCode>
            </BiographicalData>
            <AddressInformation>
                <UUID>00163e06-cc41-1ee3-bb8f-eebd7b8aa66e</UUID>
                <ValidityPeriod>
                    <StartDate>0001-01-01</StartDate>
                    <EndDate>9999-12-31</EndDate>
                </ValidityPeriod>
                <Address>
                    <PostalAddress>
                        <CountryCode>HN</CountryCode>
                        <StreetPostalCode>21101</StreetPostalCode>
                        <StreetName>Baracoa</StreetName>
                        <TimeZoneCode>UTC-6</TimeZoneCode>
                    </PostalAddress>
                    <Telephone>
                        <TelephoneFormattedNumberDescription>55555</TelephoneFormattedNumberDescription>
                        <NormalisedNumberDescription>55555</NormalisedNumberDescription>
                        <MobilePhoneNumberIndicator>true</MobilePhoneNumberIndicator>
                        <SMSEnabledIndicator>true</SMSEnabledIndicator>
                    </Telephone>
                </Address>
            </AddressInformation>
            <WorkplaceAddressInformation>
                <UUID>00163e06-cc41-1ee3-bb8f-eebd7b8ce66e</UUID>
                <WorkplaceAddressOrganisation>
                    <TypeCode>1</TypeCode>
                    <PostalAddress>
                        <CountryCode>HN</CountryCode>
                        <StreetPostalCode>21101</StreetPostalCode>
                        <StreetName>Baracoa</StreetName>
                        <TimeZoneCode>UTC-6</TimeZoneCode>
                    </PostalAddress>
                </WorkplaceAddressOrganisation>
            </WorkplaceAddressInformation>
        </EmployeeData>
        <ProcessingConditions>
            <ReturnedQueryHitsNumberValue>1</ReturnedQueryHitsNumberValue>
            <MoreHitsAvailableIndicator>false</MoreHitsAvailableIndicator>
            <LastReturnedObjectID>00163E06CC411EE3BB8FEEBD7B8A466E</LastReturnedObjectID>
        </ProcessingConditions>
    </n0:EmployeeDataByIdentificationResponse_sync>
</soap-env:Body>

,当“EndDate”字段“ValidityPeriod”的日期大于当前日期时,我希望获得“id”节点“Cost Center”字段。

为此,我有以下查询,但不适合我。

FOR r IN (
                SELECT ExtractValue(Value(p),'/OrganisationalAssignment/CostCenter/Id/text()') as CODIGO_CECO,
                       ExtractValue(Value(p),'/OrganisationalAssignment/CostCenter/Name/text()') as DESCRIPCION_CECO        
                  FROM TABLE(XMLSequence(Extract(xrespond,'/WorkAgreementData/OrganisationalAssignment'))) p
                 WHERE TO_DATE(ExtractValue(Value(p),'/OrganisationalAssignment/ValidityPeriod/EndDate/text()'),'RRRR-MM-DD') >= TRUNC(SYSDATE)
        ) LOOP    
            CECOID := r.CODIGO_CECO;
            CECODESCRIPCION := r.DESCRIPCION_CECO;
          END LOOP; 

我收到错误,表明节点返回了太多行。

当“validityPeriod”日期大于当前日期时,如何获取id字段“Cost Center”。

此致

1 个答案:

答案 0 :(得分:0)

extractValue()函数is deprecated anyway;您可以使用XMLTable()从XML中获取多个结果。

根据您发布的值,这个:

FOR r IN (
        SELECT *
        FROM XMLTable('//WorkAgreementData/OrganisationalAssignment/CostCenter'
        PASSING xrespond
        COLUMNS CODIGO_CECO VARCHAR(10) PATH 'Id',
            DESCRIPCION_CECO VARCHAR(50) PATH 'Name',
            START_DATE DATE PATH 'ValidityPeriod/StartDate',
            END_DATE DATE PATH 'ValidityPeriod/EndDate'
        )
        WHERE END_DATE >= TRUNC(SYSDATE)
    ) LOOP    
        CECOID := r.CODIGO_CECO;
        CECODESCRIPCION := r.DESCRIPCION_CECO;

        dbms_output.put_line(CECOID ||' : '|| CECODESCRIPCION
          ||' : '|| r.START_DATE ||' : '|| r.END_DATE);
      END LOOP; 

产生

PL/SQL procedure successfully completed.

LFSA11514 : Liquidos Jarabes : 15-DEC-14 : 31-DEC-99

您需要XMLTable列列表中的EndDate,以便您可以将其过滤为使用sysdate;我已经包含了StartDate,只是为了表明你真的可以。如果您需要更多数据,也可以添加其他列。

您还可以使用xs:date()fn:current-date()在XPath中进行过滤:

FOR r IN (
        SELECT *
        FROM XMLTable('for $i in //WorkAgreementData/OrganisationalAssignment/CostCenter
            where xs:date($i/ValidityPeriod/EndDate) ge fn:current-date()
            return $i'
        PASSING xrespond
        COLUMNS CODIGO_CECO VARCHAR(10) PATH 'Id',
            DESCRIPCION_CECO VARCHAR(50) PATH 'Name'
        )
...