[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Move child nodes values to attributes

Posted on 2008-10-02
1
Medium Priority
?
254 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
[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
1 Comment
 
LVL 19

Accepted Solution

by:
zc2 earned 1050 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Most of the sites are being standardized with W3C Web Standards. W3C provides lot of web standard services to the web. They have the web specification, process and documentation for all the web standards. You can apply HTML, CSS and Accessibility st…
There are two main kinds of selectors in CSS: One is base selector like h1, h2, body, table or any existing HTML tags.  For instance, the following rule sets all paragraphs (<p> elements) to red: (CODE) CSS also allows us to define our own custom …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Suggested Courses

650 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