XML парсинг с пространствами имен с использованием пакета XML в R - PullRequest
1 голос
/ 08 апреля 2020

Я пытаюсь выполнить синтаксический анализ файла xml с использованием пакета XML пакета R. Пример содержимого XML выглядит следующим образом:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Header>
      <messageContext xmlns="http://www.deltavista.com/dspone/ordercheck-if/V001">
         <credentials>
            <user>foobar</user>
            <password>barbaz</password>
         </credentials>
      </messageContext>
   </soapenv:Header>
   <soapenv:Body>
      <ns1:orderCheckResponse xmlns:ns1="http://www.deltavista.com/dspone/ordercheck-if/V001">
         <ns1:returnCode>1</ns1:returnCode>
         <ns1:product>
            <ns1:name>Consumer</ns1:name>
            <ns1:country>POL</ns1:country>
            <ns1:language>POL</ns1:language>
         </ns1:product>
         <ns1:archiveID>420</ns1:archiveID>
         <ns1:reportCreationTime>201911151220</ns1:reportCreationTime>
         <ns1:foundAddress>
            <ns1:legalForm>PERSON</ns1:legalForm>
            <ns1:address>
               <ns1:name>John</ns1:name>
               <ns1:firstName>Dow</ns1:firstName>
               <ns1:gender>MALE</ns1:gender>
               <ns1:dateOfBirth>19960410</ns1:dateOfBirth>
               <ns1:location>
                  <ns1:street>nowhere</ns1:street>
                  <ns1:house>48</ns1:house>
                  <ns1:city>farfarland</ns1:city>
                  <ns1:zip>00-500</ns1:zip>
                  <ns1:country>POL</ns1:country>
               </ns1:location>
            </ns1:address>
         </ns1:foundAddress>
         <ns1:myDecision>
            <ns1:decision>YELLOW</ns1:decision>
         </ns1:myDecision>
         <ns1:personBasicData>
            <ns1:knownSince>20181201</ns1:knownSince>
            <ns1:contact>
               <ns1:item>EMAIL</ns1:item>
               <ns1:value>foo@gmail.com</ns1:value>
            </ns1:contact>
            <ns1:contact>
               <ns1:item>PHONE</ns1:item>
               <ns1:value>123456789</ns1:value>
            </ns1:contact>
         </ns1:personBasicData>
         <ns1:decisionMatrix>
            <ns1:identificationDecision>
               <ns1:personStatus xsi:type="ns1:DecisionMatrixItemPersonStatus" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                  <ns1:partialDecision>GREEN</ns1:partialDecision>
                  <ns1:value>ADULT</ns1:value>
               </ns1:personStatus>
               <ns1:identificationType xsi:type="ns1:DecisionMatrixItemIdentificationType" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                  <ns1:partialDecision>GREEN</ns1:partialDecision>
                  <ns1:value>IDENTITY_IN_CITY</ns1:value>
               </ns1:identificationType>
               <ns1:similarHit xsi:type="ns1:DecisionMatrixItemInt" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                  <ns1:partialDecision>GREEN</ns1:partialDecision>
                  <ns1:value>0</ns1:value>
               </ns1:similarHit>
               <ns1:houseType xsi:type="ns1:DecisionMatrixItemString" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                  <ns1:partialDecision>GREEN</ns1:partialDecision>
                  <ns1:value>SHARED_USAGE</ns1:value>
               </ns1:houseType>
               <ns1:nameHint xsi:type="ns1:DecisionMatrixItemNameHint" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                  <ns1:partialDecision>GREEN</ns1:partialDecision>
                  <ns1:value>CONFIRMED</ns1:value>
               </ns1:nameHint>
               <ns1:locationIdentificationType xsi:type="ns1:DecisionMatrixItemLocationIdentificationType" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                  <ns1:partialDecision>GREEN</ns1:partialDecision>
                  <ns1:value>HOUSE_CONFIRMED</ns1:value>
               </ns1:locationIdentificationType>
            </ns1:identificationDecision>
            <ns1:solvencyDecision>
               <ns1:paymentExperience xsi:type="ns1:DecisionMatrixItemPHS" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                  <ns1:partialDecision>GREEN</ns1:partialDecision>
                  <ns1:value>NOPROBLEM</ns1:value>
               </ns1:paymentExperience>
               <ns1:externalSourcesProcessingStatus xsi:type="ns1:DecisionMatrixItemExternalProcessingStatus" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                  <ns1:partialDecision>GREEN</ns1:partialDecision>
                  <ns1:value>OK</ns1:value>
               </ns1:externalSourcesProcessingStatus>
            </ns1:solvencyDecision>
            <ns1:clientExtensionsDecision>
               <ns1:applicationFilter xsi:type="ns1:DecisionMatrixItemStringWithOverride" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                  <ns1:partialDecision>GREEN</ns1:partialDecision>
                  <ns1:value>0</ns1:value>
               </ns1:applicationFilter>
               <ns1:myScore xsi:type="ns1:DecisionMatrixItemInt" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                  <ns1:partialDecision>YELLOW</ns1:partialDecision>
                  <ns1:value>401</ns1:value>
               </ns1:myScore>
            </ns1:clientExtensionsDecision>
         </ns1:decisionMatrix>
         <ns1:paymentHistory>
            <ns1:currency>PLN</ns1:currency>
            <ns1:count>0</ns1:count>
            <ns1:dateOfLastEntry>20191111</ns1:dateOfLastEntry>
            <ns1:amountTotal>0.0</ns1:amountTotal>
            <ns1:amountTotalOpen>0.0</ns1:amountTotalOpen>
            <ns1:creditStatusMax>0</ns1:creditStatusMax>
            <ns1:masterRiskStatus>Brak danych o negatywnej historii</ns1:masterRiskStatus>
         </ns1:paymentHistory>
         <ns1:normalization>
            <ns1:searchedAddress xsi:type="ns1:SearchedAddressN" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
               <ns1:name>John</ns1:name>
               <ns1:firstName>Dow</ns1:firstName>
               <ns1:gender>MALE</ns1:gender>
               <ns1:dateOfBirth>19960410</ns1:dateOfBirth>
               <ns1:location>
                  <ns1:street>nowhere</ns1:street>
                  <ns1:house>39</ns1:house>
                  <ns1:houseExtension/>
                  <ns1:city>farfarland</ns1:city>
                  <ns1:zip>00-500</ns1:zip>
                  <ns1:country>POL</ns1:country>
               </ns1:location>
               <ns1:addressID>123</ns1:addressID>
               <ns1:unitID>123</ns1:unitID>
               <ns1:liableID>1231</ns1:liableID>
               <ns1:houseID>1232</ns1:houseID>
               <ns1:streetID>1233</ns1:streetID>
               <ns1:cityID>1234</ns1:cityID>
            </ns1:searchedAddress>
            <ns1:foundAddress>
               <ns1:addressID>1235</ns1:addressID>
               <ns1:unitID>1236</ns1:unitID>
               <ns1:liableID>1237</ns1:liableID>
               <ns1:houseID>1238</ns1:houseID>
               <ns1:streetID>1239</ns1:streetID>
               <ns1:cityID>1230</ns1:cityID>
            </ns1:foundAddress>
         </ns1:normalization>
         <ns1:clientExtensions>
            <ns1:additionalData>
               <ns1:name>pesel_verification_status</ns1:name>
               <ns1:value>1</ns1:value>
            </ns1:additionalData>
            <ns1:additionalData>
               <ns1:name>pesel_verification_execution_code</ns1:name>
               <ns1:value>200</ns1:value>
            </ns1:additionalData>
            <ns1:additionalData>
               <ns1:name>pesel_verification_codes</ns1:name>
               <ns1:value>12010; 12013</ns1:value>
            </ns1:additionalData>
         </ns1:clientExtensions>
         <ns1:executionStrategy/>
      </ns1:orderCheckResponse>
   </soapenv:Body>
</soapenv:Envelope>

Вот код, который я использую для чтения и анализа это XML содержание; str впредь:

library(XML)
foobar <- xmlInternalTreeParse(str, encoding = 'KOI8-R', useInternalNodes = F)
xmlSApply(foobar$doc$children$Envelope, function(x) xmlSApply(x, names))
xmlSApply(foobar$doc$children$Envelope, function(x) xmlSApply(x, function(x1) xmlSApply(x1, names)))

Здесь я могу проанализировать содержимое XML и попытался перебрать узлы, чтобы хотя бы напечатать имена. Однако я не мог извлечь значения изнутри, хотя я прочитал много SO вопросов и попробовал бесчисленные комбинации, используя xPathApply() et c. ( ссылка )

Любой намек на то, что я могу сделать здесь неправильно.

1 Ответ

0 голосов
/ 08 апреля 2020

Рассмотрим простой поиск XPath потомка с //*, который подтверждает пространства имен для извлечения всех имен и значений элементов. Однако, поскольку ваш XPath не ссылается ни на какие префиксы пространства имен, для этого указанного поиска c он избыточен в вызовах xpathSApply:

doc <- xmlParse(str, asText=TRUE)

nmsp <- c(soapenv = "http://schemas.xmlsoap.org/soap/envelope/",
          doc = "http://www.deltavista.com/dspone/ordercheck-if/V001",
          ns1 = "http://www.deltavista.com/dspone/ordercheck-if/V001")

# NAMED CHARACTER VECTOR OF ALL 117 ELEMENT NAMES AND VALUES
elem_vals <- setNames(xpathSApply(doc, path="//*", namespaces = nmsp, xmlValue) ,
                      xpathSApply(doc, path="//*", namespaces = nmsp, xmlName))

Вывод

Имена ( первые 20 наименований)

head(names(elem_vals), 20)
#  [1] "Envelope"           "Header"             "messageContext"     "credentials"        "user"              
#  [6] "password"           "Body"               "orderCheckResponse" "returnCode"         "product"           
# [11] "name"               "country"            "language"           "archiveID"          "reportCreationTime"
# [16] "foundAddress"       "legalForm"          "address"            "name"               "firstName"   

Значения (последние 20 наименований)

tail(elem_vals, 20)    
# streetID                                                                                                                          cityID 
# "1233"                                                                                                                            "1234" 
# foundAddress                                                                                                                   addressID 
# "123512361237123812391230"                                                                                                        "1235" 
# unitID                                                                                                                         liableID 
# "1236"                                                                                                                            "1237" 
# houseID                                                                                                                         streetID 
# "1238"                                                                                                                            "1239" 
# cityID                                                                                                                  clientExtensions 
# "1230"                              "pesel_verification_status1pesel_verification_execution_code200pesel_verification_codes12010; 12013" 
# additionalData                                                                                                                      name 
# "pesel_verification_status1"                                                                                 "pesel_verification_status" 
# value                                                                                                                     additionalData 
# "1"                                                                                               "pesel_verification_execution_code200" 
# name                                                                                                                               value 
# "pesel_verification_execution_code"                                                                                                "200" 
# additionalData                                                                                                                      name 
# "pesel_verification_codes12010; 12013"                                                                        "pesel_verification_codes" 
# value                                                                                                                 executionStrategy 
# "12010; 12013"                                                                                                                       "" 
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...