title: Importing XSD and data into R
body: <p>I generated a XML output from a database (MSSQL2014) and now would like to consume the data as well as the XSD schema information into a R data frame.</p>
  
  <p>Data Source: <code>MSSQL2014 - AdventureWorks2014 database</code></p>
  
  <p>Query executed: </p>
  
  <pre><code>select top 1 * 
  from person.person as p
    join person.EmailAddress as ea on p.businessEntityID = ea.businessentityID
    join person.PersonPhone as pphone on p.businessEntityID = pphone.businessentityID
  for XML AUTO, ELEMENTS, XMLSCHEMA('person');
  </code></pre>
  
  <p>Below is the File generate.  I tried this R code to import but it is unsuccessful.  Does anyone have a guide/tip to point me to the right direction?</p>
  
  <p>RCode:</p>
  
  <pre><code>library(XML)
  (xml_data &lt;- xmlParse(gsub("&amp;thinsp;", "", "C:\\dissertation\\smta\\indata\\01_Source_Query.XML", fixed = TRUE), asText = TRUE))
  
  xml_attrib &lt;- xpathSApply(doc=xml, path="//person",  xmlAttrs)
  
  df2 &lt;- data.frame(t(xml_attrib))
  df2
  </code></pre>
  
  <p>
  </p>
  
  <pre><code>&lt;xsd:schema targetNamespace="person" xmlns:schema="person" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:sqltypes="http://schemas.microsoft.com/sqlserver/2004/sqltypes" elementFormDefault="qualified"&gt;
    &lt;xsd:import namespace="http://schemas.microsoft.com/sqlserver/2004/sqltypes" schemaLocation="http://schemas.microsoft.com/sqlserver/2004/sqltypes/sqltypes.xsd" /&gt;
    &lt;xsd:import namespace="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ContactInfo" /&gt;
    &lt;xsd:import namespace="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ContactRecord" /&gt;
    &lt;xsd:import namespace="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ContactTypes" /&gt;
    &lt;xsd:import namespace="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/IndividualSurvey" /&gt;
    &lt;xsd:element name="p"&gt;
      &lt;xsd:complexType&gt;
        &lt;xsd:sequence&gt;
          &lt;xsd:element name="BusinessEntityID" type="sqltypes:int" /&gt;
          &lt;xsd:element name="PersonType"&gt;
            &lt;xsd:simpleType&gt;
              &lt;xsd:restriction base="sqltypes:nchar" sqltypes:localeId="1033" sqltypes:sqlCompareOptions="IgnoreCase IgnoreKanaType IgnoreWidth" sqltypes:sqlSortId="52"&gt;
                &lt;xsd:maxLength value="2" /&gt;&lt;/xsd:restriction&gt;
            &lt;/xsd:simpleType&gt;
          &lt;/xsd:element&gt;
          &lt;xsd:element name="NameStyle" type="sqltypes:bit" sqltypes:sqlTypeAlias="[AdventureWorks2014].[dbo].[NameStyle]" /&gt;
          &lt;xsd:element name="Title" minOccurs="0"&gt;
            &lt;xsd:simpleType&gt;
              &lt;xsd:restriction base="sqltypes:nvarchar" sqltypes:localeId="1033" sqltypes:sqlCompareOptions="IgnoreCase IgnoreKanaType IgnoreWidth" sqltypes:sqlSortId="52"&gt;
                &lt;xsd:maxLength value="8" /&gt;&lt;/xsd:restriction&gt;
            &lt;/xsd:simpleType&gt;
          &lt;/xsd:element&gt;
          &lt;xsd:element name="FirstName"&gt;
            &lt;xsd:simpleType sqltypes:sqlTypeAlias="[AdventureWorks2014].[dbo].[Name]"&gt;
              &lt;xsd:restriction base="sqltypes:nvarchar" sqltypes:localeId="1033" sqltypes:sqlCompareOptions="IgnoreCase IgnoreKanaType IgnoreWidth" sqltypes:sqlSortId="52"&gt;
                &lt;xsd:maxLength value="50" /&gt;&lt;/xsd:restriction&gt;
            &lt;/xsd:simpleType&gt;
          &lt;/xsd:element&gt;
          &lt;xsd:element name="MiddleName" minOccurs="0"&gt;
            &lt;xsd:simpleType sqltypes:sqlTypeAlias="[AdventureWorks2014].[dbo].[Name]"&gt;
              &lt;xsd:restri ction base="sqltypes:nvarchar" sqltypes:localeId="1033" sqltypes:sqlCompareOptions="IgnoreCase IgnoreKanaType IgnoreWidth" sqltypes:sqlSortId="52"&gt;
                &lt;xsd:maxLength value="50" /&gt;&lt;/xsd:restriction&gt;
            &lt;/xsd:simpleType&gt;
          &lt;/xsd:element&gt;
          &lt;xsd:element name="LastName"&gt;
            &lt;xsd:simpleType sqltypes:sqlTypeAlias="[AdventureWorks2014].[dbo].[Name]"&gt;
              &lt;xsd:restriction base="sqltypes:nvarchar" sqltypes:localeId="1033" sqltypes:sqlCompareOptions="IgnoreCase IgnoreKanaType IgnoreWidth" sqltypes:sqlSortId="52"&gt;
                &lt;xsd:maxLength value="50" /&gt;&lt;/xsd:restriction&gt;
            &lt;/xsd:simpleType&gt;
          &lt;/xsd:element&gt;
          &lt;xsd:element name="Suffix" minOccurs="0"&gt;
            &lt;xsd:simpleType&gt;
              &lt;xsd:restriction base="sqltypes:nvarchar" sqltypes:localeId="1033" sqltypes:sqlCompareOptions="IgnoreCase IgnoreKanaType IgnoreWidth" sqltypes:sqlSortId="52"&gt;
                &lt;xsd:maxLength value="10" /&gt;&lt;/xsd:restriction&gt;
            &lt;/xsd:simpleType&gt;
          &lt;/xsd:element&gt;
          &lt;xsd:element name="EmailPromotion" type="sqltypes:int" /&gt;
          &lt;xsd:element name="AdditionalContactInfo" minOccurs="0"&gt;
            &lt;xsd:complexType sqltypes:xmlSchemaCollection="[AdventureWorks2014].[Person].[AdditionalContactInfoSchemaCollection]"&gt;
              &lt;xsd:complexContent&gt;
                &lt;xsd:restriction base="sqltypes:xml"&gt;
                  &lt;xsd:sequence&gt;
                    &lt;xsd:any processContents="strict" minOccurs="0" maxOccurs="unbounded" namespace="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ContactInfo http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ContactRecord http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ContactTypes"
                    /&gt;&lt;/xsd:sequence&gt;
                &lt;/xsd:restriction&gt;
              &lt;/xsd:complexContent&gt;
            &lt;/xsd:complexType&gt;
          &lt;/xsd:element&gt;
          &lt;xsd:element name="Demographics" minOccurs="0"&gt;
            &lt;xsd:complexType sqltypes:xmlSchemaCollection="[AdventureWorks2014].[Person].[IndividualSurveySchemaCollection]"&gt;
              &lt;xsd:complexContent&gt;
                &lt;xsd:restriction base="sqltypes:xml"&gt;
                  &lt;xsd:sequence&gt;
                    &lt;xsd:any processContents="strict" minOccurs="0" maxOccurs="unbounded" namespace="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/IndividualSurvey" /&gt;&lt;/xsd:sequence&gt;
                &lt;/xsd:restriction&gt;
              &lt;/xsd:complexContent&gt;
            &lt;/xsd:complexType&gt;
          &lt;/xsd:element&gt;
          &lt;xsd :element name="rowguid" type="sqltypes:uniqueidentifier" /&gt;
          &lt;xsd:element name="ModifiedDate" type="sqltypes:datetime" /&gt;
          &lt;xsd:element ref="schema:ea" minOccurs="0" maxOccurs="unbounded" /&gt;&lt;/xsd:sequence&gt;
      &lt;/xsd:complexType&gt;
    &lt;/xsd:element&gt;
    &lt;xsd:element name="ea"&gt;
      &lt;xsd:complexType&gt;
        &lt;xsd:sequence&gt;
          &lt;xsd:element name="BusinessEntityID" type="sqltypes:int" /&gt;
          &lt;xsd:element name="EmailAddressID" type="sqltypes:int" /&gt;
          &lt;xsd:element name="EmailAddress" minOccurs="0"&gt;
            &lt;xsd:simpleType&gt;
              &lt;xsd:restriction base="sqltypes:nvarchar" sqltypes:localeId="1033" sqltypes:sqlCompareOptions="IgnoreCase IgnoreKanaType IgnoreWidth" sqltypes:sqlSortId="52"&gt;
                &lt;xsd:maxLength value="50" /&gt;&lt;/xsd:restriction&gt;
            &lt;/xsd:simpleType&gt;
          &lt;/xsd:element&gt;
          &lt;xsd:element name="rowguid" type="sqltypes:uniqueidentifier" /&gt;
          &lt;xsd:element name="ModifiedDate" type="sqltypes:datetime" /&gt;
          &lt;xsd:element ref="schema:pphone" minOccurs="0" maxOccurs="unbounded" /&gt;&lt;/xsd:sequence&gt;
      &lt;/xsd:complexType&gt;
    &lt;/xsd:element&gt;
    &lt;xsd:element name="pphone"&gt;
      &lt;xsd:complexType&gt;
        &lt;xsd:sequence&gt;
          &lt;xsd:element name="BusinessEntityID" type="sqltypes:int" /&gt;
          &lt;xsd:element name="PhoneNumber"&gt;
            &lt;xsd:simpleType sqltypes:sqlTypeAlias="[AdventureWorks2014].[dbo].[Phone]"&gt;
              &lt;xsd:restriction base="sqltypes:nvarchar" sqltypes:localeId="1033" sqltypes:sqlCompareOptions="IgnoreCase IgnoreKanaType IgnoreWidth" sqltypes:sqlSortId="52"&gt;
                &lt;xsd:maxLength value="25" /&gt;&lt;/xsd:restriction&gt;
            &lt;/xsd:simpleType&gt;
          &lt;/xsd:element&gt;
          &lt;xsd:element name="PhoneNumberTypeID" type="sqltypes:int" /&gt;
          &lt;xsd:element name="ModifiedDate" type="sqltypes:datetime" /&gt;&lt;/xsd:sequence&gt;
      &lt;/xsd:complexType&gt;
    &lt;/xsd:element&gt;
  &lt;/xsd:schema&gt;
  &lt;p xmlns="person"&gt;
    &lt;BusinessEntityID&gt;1&lt;/BusinessEntityID&gt;
    &lt;PersonType&gt;EM&lt;/PersonType&gt;
    &lt;NameStyle&gt;0&lt;/NameStyle&gt;
    &lt;FirstName&gt;Ken&lt;/FirstName&gt;
    &lt;MiddleName&gt;J&lt;/MiddleName&gt;
    &lt;LastName&gt;Sánchez&lt;/LastName&gt;
    &lt;EmailPromotion&gt;0&lt;/EmailPromotion&gt;
    &lt;Demographics&gt;
      &lt;IndividualSurvey xmlns="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/IndividualSurvey"&gt;
        &lt;TotalPurchaseYTD&gt;0&lt;/TotalPurchaseYTD&gt;
      &lt;/IndividualSurvey&gt;
    &lt;/Demographics&gt;
    &lt;rowguid&gt;92C4279F-1207-48A3-8448-4636514EB7E2&lt;/rowgu id&gt;
      &lt;ModifiedDate&gt;2009-01-07T00:00:00&lt;/ModifiedDate&gt;
      &lt;ea&gt;
        &lt;BusinessEntityID&gt;1&lt;/BusinessEntityID&gt;
        &lt;EmailAddressID&gt;1&lt;/EmailAddressID&gt;
        &lt;EmailAddress&gt;ken0@adventure-works.com&lt;/EmailAddress&gt;
        &lt;rowguid&gt;8A1901E4-671B-431A-871C-EADB2942E9EE&lt;/rowguid&gt;
        &lt;ModifiedDate&gt;2009-01-07T00:00:00&lt;/ModifiedDate&gt;
        &lt;pphone&gt;
          &lt;BusinessEntityID&gt;1&lt;/BusinessEntityID&gt;
          &lt;PhoneNumber&gt;697-555-0142&lt;/PhoneNumber&gt;
          &lt;PhoneNumberTypeID&gt;1&lt;/PhoneNumberTypeID&gt;
          &lt;ModifiedDate&gt;2009-01-07T00:00:00&lt;/ModifiedDate&gt;
        &lt;/pphone&gt;
      &lt;/ea&gt;
  &lt;/p&gt;
  </code></pre>