简体   繁体   English

Oracle 11g中的XML

[英]Xml in oracle 11g

Good day. 美好的一天。

I have a xml in a variable of type "xmltype". 我在类型为“ xmltype”的变量中有一个xml。 xml in question is this: 有问题的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>

and I want to get the "id" node "Cost Center" field when the date of the "EndDate" field "ValidityPeriod" is greater than the current date. 当“ EndDate”字段“ ValidityPeriod”的日期大于当前日期时,我想获取“ id”节点的“ Cost Center”字段。

For that I have the following query but is not working for me. 为此,我有以下查询,但不适用于我。

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; 

I get an error that the nodes returned too many rows. 我收到一个错误,指出节点返回太多行。

how could I do to get the id field "Cost Center" when the date "validityPeriod" is greater than the current one. 当日期“ validityPeriod”大于当前日期时,如何获取id字段“ Cost Center”。

Regards 问候

The extractValue() function is deprecated anyway ; 无论如何不建议使用 extractValue()函数; you can use XMLTable() to get multiple results from your XML. 您可以使用XMLTable()从XML中获取多个结果。

With the value you posted, this: 使用您发布的值,这是:

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; 

generates: 产生:

PL/SQL procedure successfully completed.

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

You need the EndDate in the XMLTable's column list so you can then filter that to using sysdate ; 您需要XMLTable的列列表中的EndDate ,以便随后可以使用sysdate对其进行过滤; I've included the StartDate as well just to show you can really. 我还包括了StartDate ,目的只是为了向您展示。 You can add other columns too if you need more data. 如果需要更多数据,也可以添加其他列。

You could also do the filtering in the XPath, using xs:date() and fn:current-date() : 您还可以使用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'
        )
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM