Hello Community,
I would like to import a xml file. Therefore I use the SAS XML- Mapper to create a map.
My xml files contains a few rows without any values (red line):
<teacher>
<lastname>John</lastname>
</teacher>
<n12:name/>
<landscape>
<forest>tree</forest>
</landscape>
My problem is that the XML-Mapper does not create a dataset out of those rows without any values only for the other rows. Is there any possiblity to get this done either directly in the XML-Mapper or as an automated workaround.
Thanks in advance!
Please post the data set output you desire based on the example XML file fragment that you posted.
I would like to have a dataset teacher with a column John and an empty dataset n12:name and a dataset landscape with the column tree. At the moment it works for the dataset John and for the dataset landscape, but it seems to ignore the n12:name. I would like to get also an empty dataset with the name n12:name.
I created a XML Mapper file with the SAS XML Mapper application that does almost everything you requested. I added code to you example XML fragment to make it valid XML. Here is the modified XML file:
<?xml version="1.0" encoding="iso-8859-1" ?>
<root xmlns:n12="http://www.w3.org/2001/XMLSchema">
<teacher>
<lastname>John</lastname>
</teacher>
<n12:name/>
<landscape>
<forest>tree</forest>
</landscape>
</root>
I then loaded that XML into the SAS XML Mapper application.
SAS XML Mapper steps:
This resulted in this XML Mapper file:
<?xml version="1.0" encoding="UTF-8"?>
<!-- ############################################################ -->
<!-- 2015-08-11T16:13:57 -->
<!-- SAS XML Libname Engine Map -->
<!-- Generated by XML Mapper, 903200.3.0.20120523190000_v930m2 -->
<!-- ############################################################ -->
<SXLEMAP name="test_2" version="2.1">
<NAMESPACES count="1">
<NS id="1" prefix="n12">http://www.w3.org/2001/XMLSchema</NS>
</NAMESPACES>
<!-- ############################################################ -->
<TABLE name="teacher">
<TABLE-PATH syntax="XPath">/root/teacher</TABLE-PATH>
<COLUMN name="lastname">
<PATH syntax="XPath">/root/teacher/lastname</PATH>
<TYPE>character</TYPE>
<DATATYPE>string</DATATYPE>
<LENGTH>4</LENGTH>
</COLUMN>
</TABLE>
<!-- ############################################################ -->
<TABLE name="n12_name">
<TABLE-PATH syntax="XPathENR">/root/{1}name</TABLE-PATH>
<COLUMN name="name">
<PATH syntax="XPathENR">/root/{1}name</PATH>
<TYPE>character</TYPE>
<DATATYPE>string</DATATYPE>
<LENGTH>32</LENGTH>
</COLUMN>
</TABLE>
<!-- ############################################################ -->
<TABLE name="landscape">
<TABLE-PATH syntax="XPath">/root/landscape</TABLE-PATH>
<COLUMN name="forest">
<PATH syntax="XPath">/root/landscape/forest</PATH>
<TYPE>character</TYPE>
<DATATYPE>string</DATATYPE>
<LENGTH>4</LENGTH>
</COLUMN>
</TABLE>
</SXLEMAP>
In the SAS session, I use the XMLV2 LIBNAME engine to create a libref to the XML file using the XML Mapper file just created. Running the DATASETS procedure shows the data sets created and the PRINT procedure shows what you desired. The only difference from what you specified is the name of the "n12_name" data set (as noted above).
libname x xmlv2 "My Documents/292721/test_1.xml"
xmlmap="My Documents/292721/test_2.map";
proc datasets lib=x; run; quit;
proc print data=x.n12_name; run;
proc print data=x.teacher; run;
proc print data=x.landscape; run;
Hi BILLM@SAS,
thanks for your description. It sounds like what I am looking for! I try to find time next week, if it works out for me for my real XML-FIle.
Best regards,
Eric
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.