1

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!

4
  • You have to be careful with this, because if one of the resource attributes contains a string that is not a valid element name, you will get a fatal error. Commented Nov 10, 2014 at 7:55
  • Thank you for the tip. Anything in particular I should look out for in terms of invalid strings?
    – Alex R.
    Commented Nov 11, 2014 at 1:13
  • From the spec: "The first character of a Name MUST be a NameStartChar, and any other characters MUST be NameChars;" -- Short (unofficial) version: Tag names cannot contain any of the characters !"#$%&'()*+,/;<=>?@[\]^`{|}~, nor a space character, and cannot start with -, ., or a numeric digit. Commented Nov 11, 2014 at 1:37
  • Ah, thank you. That explains why I was having trouble with trouble with the protocol name that had a space in it.
    – Alex R.
    Commented Nov 12, 2014 at 0:01

1 Answer 1

2

Try this XSLT. templates matching RESOURCEDATA and PLANETARY are defined as per your transformation needs:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="@*" >
    <xsl:element name="{name()}">
        <xsl:value-of select="."/>
    </xsl:element>
</xsl:template>

<xsl:template match="node()">
    <xsl:copy>
        <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="/PLANETARY">
    <Protocol>
        <xsl:value-of select="@Protocol"/>
        <xsl:apply-templates select="node()"/>
    </Protocol>
</xsl:template>

<xsl:template match="RESOURCEDATA">
    <xsl:element name="{@resource}">
        <xsl:value-of select="@value"/>
    </xsl:element>
</xsl:template>

</xsl:stylesheet>

The third template <xsl:template match="/PLANETARY"> creates a Protocol 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).

4
  • Superb! It works perfectly! Thank you! I thought seeing it applied to my own data would help me understand it better, but apparently not as well as I thought. The bottom half is more or less clear, but the second and third clusters are more mysterious. I'll see what I can google before bothering you further.
    – Alex R.
    Commented Nov 10, 2014 at 4:14
  • Lingamurthy, that is helpful. Thank you again. I also found this tool online that lets you see what the result of the XSLT transform is by pasting it and the XML code in; this is helping me understand what you did as well: xslttest.appspot.com I'm sorry I don't yet have enough rep to vote you up, but I very much appreciate this.
    – Alex R.
    Commented Nov 11, 2014 at 0:40
  • Lingamurthy, is it possible to get the protocol name ("Solar1" in the example) to repeat as an element so it shows up as a column in the resulting table?
    – Alex R.
    Commented Nov 12, 2014 at 0:02
  • Everything is possible.. The question isn't much clear but looks easier.. Please ask a new question with your request XML, expected output and your tried XSLT and someone will sure help you out.. Commented Nov 12, 2014 at 7:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.