Solved

Move child nodes values to attributes

Posted on 2008-10-02
1
246 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
It's sometimes a bit tricky to use date functions in Oracle BPEL. I'll explain quickly how you can add N days to the current date. In a BPEL process this can be useful, and you can adapt it to fit your needs. First of all, let's see how to add 1 …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…

706 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

15 Experts available now in Live!

Get 1:1 Help Now