парсинг xml с xml2 - PullRequest
       21

парсинг xml с xml2

0 голосов
/ 04 июля 2018

Я пытаюсь разобрать файл XML с помощью библиотеки xml2 xml_name (doc) и xml_children (doc) оба производят ожидаемый вывод, но когда я пытаюсь извлечь некоторые данные, используя xml_find_all, он возвращает {xml_nodeset (0)}, что я делаю неправильно?

library(xml2)

doc<-read_xml('<export xmlns="http://eu.europa.ec/fpi/fsd/export" generationDate="2018-06-15T19:29:31.078+02:00" globalFileId="117284">
    <sanctionEntity designationDetails="" unitedNationId="" euReferenceNumber="EU.36.64" logicalId="1">
        <regulation regulationType="amendment" organisationType="commission" publicationDate="2018-02-16" entryIntoForceDate="2018-02-16" numberTitle="2018/223 (OJ L43)" programme="ZWE" logicalId="110201">
            <publicationUrl>http://eur-lex.europa.eu/legal-content/EN/TXT/PDF/?uri=CELEX:32018R0223&amp;from=EN</publicationUrl>
        </regulation>
        <subjectType code="person" classificationCode="P"/>
        <nameAlias firstName="Robert" middleName="Gabriel" lastName="Mugabe" wholeName="Robert Gabriel Mugabe" function="Former President" gender="M" title="" nameLanguage="" strong="true" regulationLanguage="en" logicalId="1">
            <regulationSummary regulationType="amendment" publicationDate="2018-02-16" numberTitle="2018/223 (OJ L43)" publicationUrl="http://eur-lex.europa.eu/legal-content/EN/TXT/PDF/?uri=CELEX:32018R0223&amp;from=EN"/>
        </nameAlias>
        <birthdate circa="false" calendarType="GREGORIAN" city="" zipCode="" birthdate="1924-02-21" dayOfMonth="21" monthOfYear="2" year="1924" region="" place="" countryIso2Code="00" countryDescription="UNKNOWN" regulationLanguage="en" logicalId="1">
            <regulationSummary regulationType="amendment" publicationDate="2005-06-16" numberTitle="898/2005 (OJ L153)" publicationUrl="http://eur-lex.europa.eu/LexUriServ/LexUriServ.do?uri=OJ:L:2005:153:0009:0014:EN:PDF"/>
        </birthdate>
        <identification diplomatic="false" knownExpired="false" knownFalse="false" reportedLost="false" revokedByIssuer="false" issuedBy="" latinNumber="" nameOnDocument="" number="AD001095" region="" countryIso2Code="00" countryDescription="UNKNOWN" identificationTypeCode="passport" identificationTypeDescription="National passport" regulationLanguage="en" logicalId="315">
            <remark>(passport)</remark>
            <regulationSummary regulationType="amendment" publicationDate="2012-02-22" numberTitle="151/2012 (OJ L49)" publicationUrl="http://eur-lex.europa.eu/LexUriServ/LexUriServ.do?uri=OJ:L:2012:049:0002:0016:EN:PDF"/>
        </identification>
    </sanctionEntity>
    <sanctionEntity designationDate="2002-02-21" designationDetails="" unitedNationId="" euReferenceNumber="EU.36.64" logicalId="1">
        <remark>Date of designation referred to in Article 7 (2): 21.2.2002.</remark>
        <regulation regulationType="amendment" organisationType="commission" publicationDate="2002-09-13" entryIntoForceDate="2002-09-13" numberTitle="1643/2002 (OJ L247)" programme="ZWE" logicalId="1296">
            <publicationUrl>http://eur-lex.europa.eu/LexUriServ/LexUriServ.do?uri=OJ:L:2002:247:0022:0024:EN:PDF</publicationUrl>
        </regulation>
        <subjectType code="person" classificationCode="P"/>
        <nameAlias firstName="Robert" middleName="Gabriel" lastName="Mugabe" wholeName="Robert Gabriel Mugabe" function="president" gender="M" title="" nameLanguage="" strong="true" regulationLanguage="en" logicalId="101396">
            <regulationSummary regulationType="amendment" publicationDate="2002-09-13" numberTitle="1643/2002 (OJ L247)" publicationUrl="http://eur-lex.europa.eu/LexUriServ/LexUriServ.do?uri=OJ:L:2002:247:0022:0024:EN:PDF"/>
        </nameAlias>
        <birthdate circa="false" calendarType="GREGORIAN" city="Kutama" zipCode="" birthdate="1924-02-21" dayOfMonth="21" monthOfYear="2" year="1924" region="" place="" countryIso2Code="00" countryDescription="UNKNOWN" regulationLanguage="en" logicalId="101395">
            <regulationSummary regulationType="amendment" publicationDate="2002-09-13" numberTitle="1643/2002 (OJ L247)" publicationUrl="http://eur-lex.europa.eu/LexUriServ/LexUriServ.do?uri=OJ:L:2002:247:0022:0024:EN:PDF"/>
        </birthdate>
    </sanctionEntity>
    </export>
')

xml_name(doc)
xml_children(doc)

xml_find_all(doc, ".//sanctionEntity")

xml_find_all(doc, ".//publicationDate")

1 Ответ

0 голосов
/ 04 июля 2018

попробуйте использовать:

xml_find_all(doc, ".//*[name()='sanctionEntity']") 
{xml_nodeset (2)}
[1] <sanctionEntity designationDetails="" unitedNationId="" euReferenceNumber="EU.36.64" logicalId="1">\n   ...
[2] <sanctionEntity designationDate="2002-02-21" designationDetails="" unitedNationId="" euReferenceNumber= ..

xml_find_all(doc, ".//*[name()='sanctionEntity']") %>% map(xml_find_all, "./*")
[[1]]
{xml_nodeset (5)}
[1] <regulation regulationType="amendment" organisationType="commission" publicationDate="2018-02-16" entry ...
[2] <subjectType code="person" classificationCode="P"/>
[3] <nameAlias firstName="Robert" middleName="Gabriel" lastName="Mugabe" wholeName="Robert Gabriel Mugabe"  ...
[4] <birthdate circa="false" calendarType="GREGORIAN" city="" zipCode="" birthdate="1924-02-21" dayOfMonth= ...
[5] <identification diplomatic="false" knownExpired="false" knownFalse="false" reportedLost="false" revoked ...

[[2]]
{xml_nodeset (5)}
[1] <remark>Date of designation referred to in Article 7 (2): 21.2.2002.</remark>
[2] <regulation regulationType="amendment" organisationType="commission" publicationDate="2002-09-13" entry ...
[3] <subjectType code="person" classificationCode="P"/>
[4] <nameAlias firstName="Robert" middleName="Gabriel" lastName="Mugabe" wholeName="Robert Gabriel Mugabe"  ...
[5] <birthdate circa="false" calendarType="GREGORIAN" city="Kutama" zipCode="" birthdate="1924-02-21" dayOf ...
...