创建XPath以从XML中提取元素

时间:2016-12-21 22:48:31

标签: xml xpath transform transformation

我有以下示例XML,我正在尝试编写一个XPath来提取DtJobInformation密钥标记中的所有'条目。每个请求可以有多个“applicationList”标记。

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <ns2:newApp xmlns:ns2="http://abcd.com/">
            <userId>7106563</userId>
            <EDT>
                <applicantList>
                    <person>
                        <dateOfBirth>11/04/1984</dateOfBirth>
                        <firstName>Lawman</firstName>
                        <gender>Male</gender>
                        <lastName>Jeans</lastName>
                        <ssn>562-02-1254</ssn>
                    </person>
                    <personId>7106563</personId>
                    <answerSet>
                        <answers>
                            <entry>
                                <key>monthlyGrossIncome</key>
                                <value>419</value>
                            </entry>
                            <entry>
                                <key>jobTitle</key>
                                <value>Flooring</value>
                            </entry>
                            <entry>
                                <key>workOrTraining</key>
                                <value>Work</value>
                            </entry>
                            <entry>
                                <key>selfEmployment</key>
                                <value>Yes</value>
                            </entry>
                            <entry>
                                <key>hoursFrequency</key>
                                <value>Monthly</value>
                            </entry>
                        </answers>
                        <key>DtJobInformation</key>
                    </answerSet>
                    <answerSet>
                        <answers>
                            <entry>
                                <key>monthlyGrossIncome</key>
                                <value>2000</value>
                            </entry>
                            <entry>
                                <key>workOrTraining</key>
                                <value>Work</value>
                            </entry>
                            <entry>
                                <key>startDate</key>
                                <value>12/22/2016</value>
                            </entry>
                            <entry>
                                <key>employerName</key>
                                <value>Aewsome Tire Corp</value>
                            </entry>
                            <entry>
                                <key>selfEmployment</key>
                                <value>No</value>
                            </entry>
                            <entry>
                                <key>hoursFrequency</key>
                                <value>Monthly</value>
                            </entry>
                        </answers>
                        <key>DtJobInformation</key>
                    </answerSet>
                    <answerSet>
                        <answers>
                            <entry>
                                <key>employerName</key>
                                <value>Aewsome Tire Corp</value>
                            </entry>
                            <entry>
                                <key>line1</key>
                                <value>1201 Billiard ST</value>
                            </entry>
                            <entry>
                                <key>city</key>
                                <value>Peakers</value>
                            </entry>
                            <entry>
                                <key>state</key>
                                <value>KS</value>
                            </entry>
                            <entry>
                                <key>zipCode</key>
                                <value>15864</value>
                            </entry>
                        </answers>
                        <key>DtInsuranceFromJob</key>
                    </answerSet>
                </applicantList>
                <application>
                    <applicationId>9202950</applicationId>
                </application>
            </EDT>
        </ns2:newApp>
    </soap:Body>
</soap:Envelope>

这是我正在使用的XPath

Select 
w.req, 
T.* 
from mytable w, 
xmltable(xmlnamespaces( 'http://schemas.xmlsoap.org/soap/envelope/' AS "soap", 'http://abcd.com/' AS "ns2"), 
'for $txfr in .
for $txfr_hdr in $txfr/soap:Envelope/soap:Body/ns2:newApp/EDT/applicantList
return
<txfr>
{$txfr_hdr} 
</txfr>'
passing xmltype.createxml(w.req) 
COLUMNS 
personId VARCHAR(30) PATH 'txfr_hdr/personId'
) AS T;

1 个答案:

答案 0 :(得分:0)

所以你在for循环中有两个选项

选项1:使用XPath过滤所需的answerSet

 for $txfr_hdr in $txfr/soap:Envelope/soap:Body/ns2:newApp/EDT/applicantList/answerSet[key/text()='DtJobInformation']/answers
 return
  <txfr>
   {$txfr_hdr} 
  </txfr>

$ txfr_hdr现在是answers

选项2:使用for for

for $txfr_hdr in $txfr/soap:Envelope/soap:Body/ns2:newApp/EDT/applicantList/answerSet
where $txfr_hdr/key/text()='DtJobInformation'
return
  <txfr>
   {$txfr_hdr/answers} 
  </txfr>