A Tech Log

October 1, 2008

XSLT to Convert XML Elements to Attributes

Filed under: Development — adallow @ 4:01 am

David Carlisle has a good script:

If you want to convert the following from this:

<atom  phase="gas">
   <boiling_point units="Kelvin">20.28</boiling_point>

To this:
<atom phase="gas" name="Hydrogen" symbol="H">
    <boiling_point units="Kelvin">20.28</boiling_point>

Then the following XSLT can help:

<?xml version=’1.0′ encoding=’utf-8′?>
<xsl:stylesheet version=’1.0′ xmlns:xsl=’http://www.w3.org/1999/XSL/Transform&#8217; xmlns:msxsl=’urn:schemas-microsoft-com:xslt’ exclude-result-prefixes=’msxsl’>
<xsl:template match=’*’>
<xsl:for-each select=’@*|*[not(* or @*)]’>
<xsl:attribute name='{name(.)}’><xsl:value-of select=’.’/>
<xsl:apply-templates select=’*[* or @*]|text()’/>


Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: