如何使用ORACLE SQL从XML中单独提取可重复的json节点值?

时间:2017-02-28 12:37:46

标签: sql json xml oracle

我有以下XML,我想分别提取json参数“serviceNumber”的值我尝试使用EXTRACT函数,但我得到了连接的结果,但我希望它们分开。

<Input>
   <Header>
      <User-Agent></User-Agent>
      <Accepted-Language></Accepted-Language>
      <Authorization></Authorization>
      <Date>Mon, 27 Feb 2017 21:39:55 GMT</Date>
      <Username></Username>
      <Test />
      <UserInfo />
      <msisdn />
      <imsi />
      <X-Forwarded-For></X-Forwarded-For>
      <x-Client-IP></x-Client-IP>
      <Location />
   </Header>
   <URI></URI>
   <ServiceName></ServiceName>
   <PrimaryKey></PrimaryKey>
   <Parameters>
      <Parameter1></Parameter1>
      <Parameter2 />
      <Parameter3 />
   </Parameters>
   <Body>
      <json:object xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.datapower.com/schemas/json jsonx.xsd">
         <json:string name="transactionType"></json:string>
         <json:array name="payments">
            <json:object>
               <json:string name="amount"></json:string>
               <json:string name="serviceNumber">536888669</json:string>
               <json:string name="serviceType"></json:string>
               <json:string name="accountNumber"></json:string>
            </json:object>
            <json:object>
               <json:string name="amount"></json:string>
               <json:string name="serviceNumber">535394441</json:string>
               <json:string name="serviceType"></json:string>
               <json:string name="accountNumber"></json:string>
            </json:object>
            <json:object>
               <json:string name="amount"></json:string>
               <json:string name="serviceNumber">138422262</json:string>
               <json:string name="serviceType"></json:string>
               <json:string name="accountNumber"></json:string>
            </json:object>
            <json:object>
               <json:string name="amount"></json:string>
               <json:string name="serviceNumber">505809543</json:string>
               <json:string name="serviceType"></json:string>
               <json:string name="accountNumber"></json:string>
            </json:object>
            <json:object>
               <json:string name="amount"></json:string>
               <json:string name="serviceNumber">138422338</json:string>
               <json:string name="serviceType"></json:string>
               <json:string name="accountNumber"></json:string>
            </json:object>
         </json:array>
      </json:object>
   </Body>
</Input>

我希望输出像

serviceNumber
-------------
536888669
535394441
138422262
505809543
138422338

2 个答案:

答案 0 :(得分:0)

使用{ "name": "my-app", "version": "0.1.0", "description": "", "private":true, "scripts": { "start": "tsc && concurrently \"tsc -w\" \"lite-server\" ", "lite": "lite-server", "tsc": "tsc", "tsc:w": "tsc -w" }, "dependencies": { "@angular/common": "^2.4.0", "@angular/compiler": "^2.4.0", "@angular/core": "^2.4.0", "@angular/forms": "^2.4.0", "@angular/http": "^2.4.0", "@angular/platform-browser": "^2.4.0", "@angular/platform-browser-dynamic": "^2.4.0", "@angular/router": "^3.4.0", "core-js": "^2.4.1", "rxjs": "^5.0.0", "ts-helpers": "^1.1.1", "zone.js": "^0.7.7" }, "devDependencies": { "@angular/cli": "1.0.0-beta.31", "@angular/compiler-cli": "^2.4.0", "@types/jasmine": "2.5.38", "@types/node": "^6.0.42", "codelyzer": "~2.0.0-beta.1", "jasmine-core": "2.5.2", "jasmine-spec-reporter": "2.5.0", "karma": "1.2.0", "karma-chrome-launcher": "^2.0.0", "karma-cli": "^1.0.1", "karma-jasmine": "^1.0.2", "karma-coverage-istanbul-reporter": "^0.2.0", "protractor": "~5.1.0", "ts-node": "1.2.1", "tslint": "^4.3.0", "typescript": "~2.0.0" } }

XMLTABLE

答案 1 :(得分:0)

尝试XMLTABLE

select * from xmltable(XMLNAMESPACES( 'http://www.ibm.com/xmlns/prod/2009/jsonx' as "json") , '//json:string[@name eq "serviceNumber"]' passing xmltype({your_xml_here})
 columns serviceNumber varchar2(100) path '.' 
 )