Solved

XML: writing xslt to transform xmlelement into xmlattribute

Posted on 2008-06-11
5
277 Views
Last Modified: 2013-11-26
Existing XML:

<ROOT>
   <Element1>
      <Element2>
         Value
      </Element2>
   </Element1>
</ROOT>

Transformed XML should be like this:
<ROOT>
   <Element1 Element2="Value">
   </Element1>
</ROOT>

Thank you for your help.



0
Comment
Question by:quasar_ee
5 Comments
 
LVL 11

Expert Comment

by:kmartin7
ID: 21765866
There are several ways to do what you are asking. If you want a static method, then the following will work:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >

  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/">
      <xsl:apply-templates />
  </xsl:template>

<xsl:template match="ROOT">
            <xsl:apply-templates />
      </xsl:template>


<xsl:template match="Element1">
      <Element1>
            <xsl:attribute name="Element2"><xsl:value-of select="Element2"/></xsl:attribute>
      </Element1>
</xsl:template>

</xsl:stylesheet>

You can actually match many elements and use the value of name() or local-name(), and grab the following element's value as an attribute value. Without knowing more about what your specific needs are, the above is a rudimentary but working example.

HTH,

kmartin7
0
 

Author Comment

by:quasar_ee
ID: 21766072
Thank you kmartin7.
Existing XML is actually like this:
<ROOT>
   <Element1>
      <Element2>
         Value
      </Element2>
   </Element1>
   <Element3>Value</Element3>
   ...
   <ElementN>Value</ElementN>
</ROOT>

Element2 will be always the same. All other elements can have different names in different XML files. I need solution to be as generic as possible.
0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 250 total points
ID: 21766466
Here is a more generic version that makes an identity copy but trasforms all Element2 in an attribute of the parent

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="node()">
    <xsl:copy>
        <xsl:copy-of select="@*"/>
        <xsl:if test="Element2">
            <xsl:attribute name="Element2">
                <xsl:value-of select="normalize-space(Element2)"/>
            </xsl:attribute>
        </xsl:if>
        <xsl:apply-templates select="node()"></xsl:apply-templates>
    </xsl:copy>
</xsl:template>
    <xsl:template match="Element2"></xsl:template>
</xsl:stylesheet>
0
 
LVL 10

Expert Comment

by:margajet24
ID: 21776002
so you would like it to iterate to all nodes ?
0
 
LVL 11

Expert Comment

by:kmartin7
ID: 21778182
quasar_ee:

Geert's solution will work for you, regarding your last comment.

kmartin7
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question