Solved

XML: writing xslt to transform xmlelement into xmlattribute

Posted on 2008-06-11
5
279 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

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…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
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 …

752 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