Solved

XML: writing xslt to transform xmlelement into xmlattribute

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now