1

我是 Oracle XML 领域的新手。我正在尝试解析 XML,但没有做到。

下面是 XML 数据。


<CompositeDataObject Type="ApplicantsAAT" Version="0">
<DataObjectElement SystemName="AccountRelationship" ObjectType="DataObjectCollection">
<CollectionItem ID="604008" EncryptedID="4GnX1!b8epwsAmwbfvZjo;xAAAAAB6r2wy" DataObjectCategory="RepeatingAttribute" DataObjectType="AccountRelationshipAAT"/>
<CollectionItem ID="604040" EncryptedID="4IrPkqeoo7EVLEuSm7egK96AAAAAB6r2zS" DataObjectCategory="RepeatingAttribute" DataObjectType="AccountRelationshipAAT"/>
</DataObjectElement>
<DataObjectElement SystemName="ApplicantCreditRefDetailsAATSL" ObjectType="DataObject" ID="603979" EncryptedID="4PPEFaEyt2Q!K48f13wy7gzAAAAAB6r2wR" DataObjectCategory="RepeatingAttribute" DataObjectType="ApplicantCreditRefDetailsAATSL"/><DataObjectElement SystemName="CreditCardsTPF" ObjectType="DataObjectCollection"/><CompositeDataObjectElement SystemName="CreditObligations" ObjectType="DataObjectCollection"/><DataObjectElement SystemName="CreditScoring" ObjectType="DataObject" ID="603970" EncryptedID="4CWx;PRGqbV1IMzP4DtnY2IAAAAAB6r2wY" DataObjectCategory="RepeatingAttribute" DataObjectType="CreditScoring"/><DataObjectElement SystemName="DependantDetails" ObjectType="DataObject" ID="603978" EncryptedID="4OReRldXZeF8IU9a2e3RZk9AAAAAB6r2wQ" DataObjectCategory="RepeatingAttribute" DataObjectType="ApplicantDependantDetailsAATSL"/><DataObjectElement SystemName="EmploymentAATSL" ObjectType="DataObject" ID="603972" EncryptedID="4GkF83Ll!xLiLqS1zL;FMG4AAAAAB6r2we" DataObjectCategory="RepeatingAttribute" DataObjectType="EmploymentAperioOnlyAATSL"/><DataObjectElement SystemName="ExpenditureDetailsTPF" ObjectType="DataObject" ID="603980" EncryptedID="4G6EGC6aL8LmN94xr9LNgRxAAAAAB6r2wW" DataObjectCategory="RepeatingAttribute" DataObjectType="ExpenditureTPF"/><DataObjectElement SystemName="Fields" ObjectType="DataObject" ID="9251" EncryptedID="4MEw5;;AiwprG2uKYJGudrLAAAAAB6pn95" DataObjectCategory="Party" DataObjectType="Personal"/><DataObjectElement SystemName="FinancialAssetsAATSL" ObjectType="DataObjectCollection"/><DataObjectElement SystemName="FinancialExpenseAATSL" ObjectType="DataObjectCollection"/><DataObjectElement SystemName="FinancialLiabilitiesAATSL" ObjectType="DataObjectCollection"/><DataObjectElement SystemName="FinancierDeductionAATSL" ObjectType="DataObjectCollection"/><DataObjectElement SystemName="IncomeSalariedAATSL" ObjectType="DataObject" ID="603973" EncryptedID="4J04bAAaMKqPIXbr7AAUfoQAAAAAB6r2wf" DataObjectCategory="RepeatingAttribute" DataObjectType="ApplicantFinancialIncomeSalariedFinalAATSL"/><DataObjectElement SystemName="IncomeSelfEmployedAATSL" ObjectType="DataObject" ID="603974" EncryptedID="4D;pvvFrYPDyLjkipR7sYyQAAAAAB6r2wc" DataObjectCategory="RepeatingAttribute" DataObjectType="ApplicantFinancialIncomeSelfEmplAATAP"/>
<CompositeDataObjectElement SystemName="Tabs" ObjectType="DataObject" ID="378409" EncryptedID="4J!pTqS7x4XxIYZjRd2jA2UAAAAAB6o51z" CompositeDataObject="ApplicantsViewsAAT"/>
</CompositeDataObject>

CollectionItem如果内部仅存在一次,则以下查询可以正常工作DataObjectElement。如果collectionItem出现不止一次,则失败。基本上我想要实现的是在看到标签 ID 时将数据考虑到新行中。


 WITH t AS (SELECT XMLType('<CompositeDataObject Type="ApplicantsAAT" Version="0">
<DataObjectElement SystemName="AccountRelationship" ObjectType="DataObjectCollection">
<CollectionItem ID="604008" EncryptedID="4GnX1!b8epwsAmwbfvZjo;xAAAAAB6r2wy" DataObjectCategory="RepeatingAttribute" DataObjectType="AccountRelationshipAAT"/>
<CollectionItem ID="604040" EncryptedID="4IrPkqeoo7EVLEuSm7egK96AAAAAB6r2zS" DataObjectCategory="RepeatingAttribute" DataObjectType="AccountRelationshipAAT"/>
</DataObjectElement>
<DataObjectElement SystemName="ApplicantCreditRefDetailsAATSL" ObjectType="DataObject" ID="603979" EncryptedID="4PPEFaEyt2Q!K48f13wy7gzAAAAAB6r2wR" DataObjectCategory="RepeatingAttribute" DataObjectType="ApplicantCreditRefDetailsAATSL"/><DataObjectElement SystemName="CreditCardsTPF" ObjectType="DataObjectCollection"/><CompositeDataObjectElement SystemName="CreditObligations" ObjectType="DataObjectCollection"/><DataObjectElement SystemName="CreditScoring" ObjectType="DataObject" ID="603970" EncryptedID="4CWx;PRGqbV1IMzP4DtnY2IAAAAAB6r2wY" DataObjectCategory="RepeatingAttribute" DataObjectType="CreditScoring"/><DataObjectElement SystemName="DependantDetails" ObjectType="DataObject" ID="603978" EncryptedID="4OReRldXZeF8IU9a2e3RZk9AAAAAB6r2wQ" DataObjectCategory="RepeatingAttribute" DataObjectType="ApplicantDependantDetailsAATSL"/><DataObjectElement SystemName="EmploymentAATSL" ObjectType="DataObject" ID="603972" EncryptedID="4GkF83Ll!xLiLqS1zL;FMG4AAAAAB6r2we" DataObjectCategory="RepeatingAttribute" DataObjectType="EmploymentAperioOnlyAATSL"/><DataObjectElement SystemName="ExpenditureDetailsTPF" ObjectType="DataObject" ID="603980" EncryptedID="4G6EGC6aL8LmN94xr9LNgRxAAAAAB6r2wW" DataObjectCategory="RepeatingAttribute" DataObjectType="ExpenditureTPF"/><DataObjectElement SystemName="Fields" ObjectType="DataObject" ID="9251" EncryptedID="4MEw5;;AiwprG2uKYJGudrLAAAAAB6pn95" DataObjectCategory="Party" DataObjectType="Personal"/><DataObjectElement SystemName="FinancialAssetsAATSL" ObjectType="DataObjectCollection"/><DataObjectElement SystemName="FinancialExpenseAATSL" ObjectType="DataObjectCollection"/><DataObjectElement SystemName="FinancialLiabilitiesAATSL" ObjectType="DataObjectCollection"/><DataObjectElement SystemName="FinancierDeductionAATSL" ObjectType="DataObjectCollection"/><DataObjectElement SystemName="IncomeSalariedAATSL" ObjectType="DataObject" ID="603973" EncryptedID="4J04bAAaMKqPIXbr7AAUfoQAAAAAB6r2wf" DataObjectCategory="RepeatingAttribute" DataObjectType="ApplicantFinancialIncomeSalariedFinalAATSL"/><DataObjectElement SystemName="IncomeSelfEmployedAATSL" ObjectType="DataObject" ID="603974" EncryptedID="4D;pvvFrYPDyLjkipR7sYyQAAAAAB6r2wc" DataObjectCategory="RepeatingAttribute" DataObjectType="ApplicantFinancialIncomeSelfEmplAATAP"/>
<CompositeDataObjectElement SystemName="Tabs" ObjectType="DataObject" ID="378409" EncryptedID="4J!pTqS7x4XxIYZjRd2jA2UAAAAAB6o51z" CompositeDataObject="ApplicantsViewsAAT"/>
</CompositeDataObject>') xmlcol
               FROM dual)
    SELECT extract(value(x),'//*[@ID]/@ID') linked_object_id,
    extractValue(value(x),'//*[@ID]/ancestor-or-self::*[@EncryptedID]/@EncryptedID') linked_object_encrypted_id,
    extractValue(value(x),'//*[@ID]/ancestor-or-self::*[@SystemName]/@SystemName') system_name,
    extractValue(value(x),'//*[@ID]/ancestor-or-self::*[@DataObjectCategory]/@DataObjectCategory') data_object_category,
    extractValue(value(x),'//*[@ID]/ancestor-or-self::*[@ObjectType]/@ObjectType') object_type,
    extractValue(value(x),'//*[@ID]/ancestor-or-self::*[@DataObjectType]/@DataObjectType') data_object_type,
     extractValue(value(x),'//*[@ID]/ancestor-or-self::*[@CompositeDataObject]/@CompositeDataObject') composite_data_object
    FROM t, TABLE(XMLSequence(extract(t.xmlcol,'/CompositeDataObject/*'))) x;

====================================== 下面是我期待的简单示例

感谢您的宝贵回复。

您发布的查询工作正常。但正如您所看到的,有些字段生成为 NULL。例如。第一个 DataObjectElement 包含 2 个 CollectionItem(604008,604008a),但 CollectionItem 中没有“SystemName”,所以我期待主 DataObjectElement 标记中的 SystemName .. 即“AccountRelationship”。对象类型也是如此

WITH t AS (SELECT XMLType('<CompositeDataObject Type="ApplicantsAAT" Version="0">
<DataObjectElement SystemName="AccountRelationship" ObjectType="DataObjectCollection">
<CollectionItem ID="604008" EncryptedID="4GnX1!b8epwsAmwbfvZjo;xAAAAAB6r2wy" DataObjectCategory="RepeatingAttribute" DataObjectType="AccountRelationshipAAT"/>
<CollectionItem ID="604008a" EncryptedID="4GnX1!b8epwsAmwbfvZjo;xAAAAAB6r2wy" DataObjectCategory="RepeatingAttribute" DataObjectType="AccountRelationshipAAT"/>
</DataObjectElement>
<DataObjectElement SystemName="ApplicantCreditRefDetailsAATSL" ObjectType="DataObject" ID="603979" EncryptedID="4PPEFaEyt2Q!K48f13wy7gzAAAAAB6r2wR" DataObjectCategory="RepeatingAttribute" DataObjectType="ApplicantCreditRefDetailsAATSL"/>
<CompositeDataObjectElement SystemName="Tabs" ObjectType="DataObject" ID="378409" EncryptedID="4J!pTqS7x4XxIYZjRd2jA2UAAAAAB6o51z" CompositeDataObject="ApplicantsViewsAAT"/>
</CompositeDataObject>') xmlcol
               FROM dual)

                 SELECT extractvalue(value(x), '/*/@ID') id,
              extractvalue(value(x), '/*/@EncryptedID') EncryptedID,
              extractvalue(value(x), '/*/@SystemName') SystemName,
              extractvalue(value(x), '/*/@DataObjectCategory') DataObjectCategory,
              extractvalue(value(x), '/*/@ObjectType') ObjectType,
              extractvalue(value(x), '/*/@DataObjectType') DataObjectType,
              extractvalue(value(x), '/*/@CompositeDataObject') CompositeDataObject
       FROM t, TABLE(XMLSequence(extract(t.xmlcol,'//*[@ID]'))) x;

上述查询的结果是

ID      ENCRYPTEDID                         SYSTEMNAME                      DATAOBJECTCATEGORY  OBJECTTYPE              DATAOBJECTTYPE                  COMPOSITEDATAOBJECT
604008  4GnX1!b8epwsAmwbfvZjo;xAAAAAB6r2wy                                  RepeatingAttribute                          AccountRelationshipAAT  
604008a 4GnX1!b8epwsAmwbfvZjo;xAAAAAB6r2wy                                  RepeatingAttribute                          AccountRelationshipAAT  
603979  4PPEFaEyt2Q!K48f13wy7gzAAAAAB6r2wR  ApplicantCreditRefDetailsAATSL  RepeatingAttribute  DataObject  ApplicantCreditRefDetailsAATSL  
378409  4J!pTqS7x4XxIYZjRd2jA2UAAAAAB6o51z  Tabs                                                DataObject                                                                              ApplicantsViewsAAT

我期待的只是轻微的变化(下面的结果包含 SYSTEMNAME 和 OBJECTTYPE 的值)

ID      ENCRYPTEDID                         SYSTEMNAME                      DATAOBJECTCATEGORY  OBJECTTYPE              DATAOBJECTTYPE                  COMPOSITEDATAOBJECT
604008  4GnX1!b8epwsAmwbfvZjo;xAAAAAB6r2wy  AccountRelationship             RepeatingAttribute  DataObjectCollection    AccountRelationshipAAT  
604008a 4GnX1!b8epwsAmwbfvZjo;xAAAAAB6r2wy  AccountRelationship             RepeatingAttribute  DataObjectCollection    AccountRelationshipAAT  
603979  4PPEFaEyt2Q!K48f13wy7gzAAAAAB6r2wR  ApplicantCreditRefDetailsAATSL  RepeatingAttribute  DataObject  ApplicantCreditRefDetailsAATSL  
378409  4J!pTqS7x4XxIYZjRd2jA2UAAAAAB6o51z  Tabs                                                DataObject                                                                              ApplicantsViewsAAT

请把我的内容复制到一些文本板中以获得更清晰的视图。非常感谢您的帮助

4

1 回答 1

0

im not totally sure what you're after but is it 1 row per "id"? if so..

 10  select id, EncryptedID,
 11         nvl(SystemName, extractvalue(xmlcol, '//*[child::*/@ID="' || id || '"]/@SystemName')) SystemName,
 12         DataObjectCategory,
 13         nvl(ObjectType, extractvalue(xmlcol, '//*[child::*/@ID="' || id || '"]/@ObjectType')) ObjectType,
 14         DataObjectType,
 15         CompositeDataObject
 16    from (SELECT t.xmlcol, extractvalue(value(x), '/*/@ID') id,
 17                 extractvalue(value(x), '/*/@EncryptedID') EncryptedID,
 18                 extractvalue(value(x), '/*/@SystemName') SystemName,
 19                 extractvalue(value(x), '/*/@DataObjectCategory') DataObjectCategory,
 20                 extractvalue(value(x), '/*/@ObjectType') ObjectType,
 21                 extractvalue(value(x), '/*/@DataObjectType') DataObjectType,
 22                 extractvalue(value(x), '/*/@CompositeDataObject') CompositeDataObject
 23          FROM t, TABLE(XMLSequence(extract(t.xmlcol,'//*[@ID]'))) x)
 24  ;

ID         ENCRYPTEDID          SYSTEMNAME           DATAOBJECTCATEGORY   OBJECTTYPE           DATAOBJECTTYPE       COMPOSITEDATAOBJECT
---------- -------------------- -------------------- -------------------- -------------------- -------------------- --------------------
604008     4GnX1!b8epwsAmwbfvZj AccountRelationship  RepeatingAttribute   DataObjectCollection AccountRelationshipA
           o;xAAAAAB6r2wy                                                                      AT

604040     4IrPkqeoo7EVLEuSm7eg AccountRelationship  RepeatingAttribute   DataObjectCollection AccountRelationshipA
           K96AAAAAB6r2zS                                                                      AT

603979     4PPEFaEyt2Q!K48f13wy ApplicantCreditRefDe RepeatingAttribute   DataObject           ApplicantCreditRefDe
           7gzAAAAAB6r2wR       tailsAATSL                                                     tailsAATSL

603970     4CWx;PRGqbV1IMzP4Dtn CreditScoring        RepeatingAttribute   DataObject           CreditScoring
           Y2IAAAAAB6r2wY

603978     4OReRldXZeF8IU9a2e3R DependantDetails     RepeatingAttribute   DataObject           ApplicantDependantDe
           Zk9AAAAAB6r2wQ                                                                      tailsAATSL

603972     4GkF83Ll!xLiLqS1zL;F EmploymentAATSL      RepeatingAttribute   DataObject           EmploymentAperioOnly
           MG4AAAAAB6r2we                                                                      AATSL

603980     4G6EGC6aL8LmN94xr9LN ExpenditureDetailsTP RepeatingAttribute   DataObject           ExpenditureTPF
           gRxAAAAAB6r2wW       F

9251       4MEw5;;AiwprG2uKYJGu Fields               Party                DataObject           Personal
           drLAAAAAB6pn95

603973     4J04bAAaMKqPIXbr7AAU IncomeSalariedAATSL  RepeatingAttribute   DataObject           ApplicantFinancialIn
           foQAAAAAB6r2wf                                                                      comeSalariedFinalAAT
                                                                                               SL

603974     4D;pvvFrYPDyLjkipR7s IncomeSelfEmployedAA RepeatingAttribute   DataObject           ApplicantFinancialIn
           YyQAAAAAB6r2wc       TSL                                                            comeSelfEmplAATAP

378409     4J!pTqS7x4XxIYZjRd2j Tabs                                      DataObject                                ApplicantsViewsAAT
           A2UAAAAAB6o51z
于 2013-01-04T21:04:13.890 回答