Solved

Move child nodes values to attributes

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
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 …

730 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