ulrich
ulrich

Reputation: 3587

parsing xml with xml2

I am trying to parse an XML file using the xml2 library xml_name(doc) and xml_children(doc) are both producing the expected output but when I try to extract some data using xml_find_all it returns {xml_nodeset (0)} what am I doing wrong?

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")

Upvotes: 0

Views: 254

Answers (2)

G. Grothendieck
G. Grothendieck

Reputation: 269431

The problem is that the XML document uses a namespace. Either prefix the tag name with the namespace prefix or strip the document of namespaces.

xml_ns(doc)  # namespace present with prefix d1
## d1 <-> http://eu.europa.ec/fpi/fsd/export
doc |> xml_find_all("d1:sanctionEntity")
## {xml_nodeset (2)}
## [1] <sanctionEntity designationDetails="" unitedNationId="" euReferenceNumber ...
## [2] <sanctionEntity designationDate="2002-02-21" designationDetails="" united ...

# strip namespace
doc |> xml_ns_strip() |> xml_find_all("sanctionEntity")
## {xml_nodeset (2)}
## [1] <sanctionEntity designationDetails="" unitedNationId="" euReferenceNumber ...
## [2] <sanctionEntity designationDate="2002-02-21" designationDetails="" uni

Upvotes: 1

Kestutis Vinciunas
Kestutis Vinciunas

Reputation: 63

try to use:

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 ...

Upvotes: 1

Related Questions