Solved

XML: writing xslt to transform xmlelement into xmlattribute

Posted on 2008-06-11
5
278 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

Title # Comments Views Activity
Object Oriented Programming, C#, referencing, scoping. 13 69
parsing JSON help 1 20
Upgrading my SSIS package in VS 2012 6 62
Generate Unique ID in VB.NET 21 68
Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa‚Ķ
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

820 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