Solved

Move child nodes values to attributes

Posted on 2008-10-02
1
247 Views
Last Modified: 2013-11-18
Hi experts,

I have the following xml file:
--------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<RootCircle  version="1.0">
  <Square  size = "m">
    <myExtension name = "angle" value = "4" />
    <Triangle color="red" size="big" />
    <Triangle  />
  </Square>
</RootCircle>
----------------------------------------
I want the output to be:

<?xml version="1.0" encoding="UTF-8"?>
<RootCircle  version="1.0">
  <Square  size = "m"  angle = "4">
    <Triangle color="red" size="big" />
    <Triangle  />
  </Square>
</RootCircle>

------------------------------
How do I do this? Thanks a lot!

0
Comment
Question by:meow00
1 Comment
 
LVL 18

Accepted Solution

by:
zc2 earned 350 total points
ID: 22629850
the xsl below replaces each myExtension element to the corresponding attribute of its parent
<?xml version="1.0"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

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

	<xsl:template match="myExtension">

		<xsl:attribute name="{@name}"><xsl:value-of select="@value"/></xsl:attribute>

	</xsl:template>
 

	<xsl:template match="*[myExtension]">

	  <xsl:copy>

	    <xsl:apply-templates select="@*"/>

		<xsl:apply-templates select="myExtension"/>

		<xsl:apply-templates select="*[name()!='myExtension']"/>

	  </xsl:copy>

	</xsl:template>

	

	<xsl:template match="*">

		<xsl:copy>

			<xsl:apply-templates select="@*|*|text()"/>

		</xsl:copy>

 	</xsl:template>

	

	<xsl:template match="@*|text()">

		<xsl:copy/>

 	</xsl:template>

	

</xsl:stylesheet>

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Node js - send response after all promises are resolved 1 51
WordPress/WooCommerce security best practices? 2 82
Lines Not Lining Up 2 62
Problem to echo 6 80
Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

912 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

22 Experts available now in Live!

Get 1:1 Help Now