I've seen a few other answers for similar answers, but I can't quite wrap my head around it. I have an attribute centric XML file that I need to import into Access, which will only accept element centric formatting. It seems like I need to do a transform with an XSL file, but I'm not clear how to do it. Since the data is proprietary, I've disguised it with a sci fi theme. What I need to do is turn the first code example into the same format as the second one:
<PLANETARY Protocol="Solar 1">
<COLONIES>
<COLONYDATA site="10001" planet="Mars">
<RESOURCEDATA resource="RadiationDanger" value="Low" />
<RESOURCEDATA resource="ApplicantColonists" value="11" />
<RESOURCEDATA resource="AcceptedColonists" value="3" />
</COLONYDATA>
<COLONYDATA site="10002" planet="Mars">
<RESOURCEDATA resource="RadiationDanger" value="Low" />
<RESOURCEDATA resource="ApplicantColonists" value="7" />
<RESOURCEDATA resource="AcceptedColonists" value="1" />
</COLONYDATA>
<COLONYDATA site="11019" planet="Titan">
<RESOURCEDATA resource="RadiationDanger" value="Low" />
<RESOURCEDATA resource="ApplicantColonists" value="22" />
<RESOURCEDATA resource="AcceptedColonists" value="16" />
</COLONYDATA>
</COLONIES>
</PLANETARY>
<Protocol>
Solar1
<COLONIES>
<COLONYDATA>
<site>10001</site>
<planet>Mars</planet>
<RadiationDanger>Low</RadiationDanger>
<ApplicantColonists>11</ApplicantColonists>
<AcceptedColonists>3</AcceptedColonists>
</COLONYDATA>
<COLONYDATA>
<site>10002</site>
<planet>Mars</planet>
<RadiationDanger>Low</RadiationDanger>
<ApplicantColonists>7</ApplicantColonists>
<AcceptedColonists>1</AcceptedColonists>
</COLONYDATA>
<COLONYDATA>
<site>11019</site>
<planet>Titan</planet>
<RadiationDanger>Low</RadiationDanger>
<ApplicantColonists>22</ApplicantColonists>
<AcceptedColonists>16</AcceptedColonists>
</COLONYDATA>
</COLONIES>
</Protocol>
Any help would be appreciated. Thanks!
Try this XSLT.
templates
matchingRESOURCEDATA
andPLANETARY
are defined as per your transformation needs:The third template
<xsl:template match="/PLANETARY">
creates aProtocol
element, adds@Protocol
value, and applies templates to its nodes(i.e., in this case, applying templates to its child elements).The fouth template
<xsl:template match="RESOURCEDATA">
creates an element with name as@resource
's value, and value from@value
.Coming to the first two templates, first one (
<xsl:template match="@*" >
) when applied to any attribute converts it into an element(element-centric).And the second template,
<xsl:template match="@*" >
matches nodes(in your case elements), copy the tags, and apply templates for its attribtues and child elements(in a recursive way).