473,387 Members | 1,515 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Trouble converting elements to attributes with XSLT

Hello,

I'm having a bit of difficulty transforming xml elements into attributes.

I want to loop through all of the <person> elements in the document and turn all of its child elements into attributes of the person element.

The code i am using takes the last <person> element and changes all of its elements into attributes of the root node and then removes all of child elements of every person element.


This is the code that i am currently using.



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

<xsl:output method="xml" encoding="UTF-8" indent="yes"/>

<xsl:template match="*">

<xsl:copy>
<xsl:for-each select="person/*">
<xsl:attribute name="{name(.)}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:for-each>
<xsl:apply-templates select="*[* or @*]|text()"/>
</xsl:copy>
</xsl:template>


</xsl:stylesheet>



the base xml document looks like this:


<root>
<person>
<name>Johnny</name>
<id>1</id>
<departme#nt>CEO</sales>
<reports-to>null</reports-to>
</person>
<person>
<name>Madge</name>
<id>2</id>
<departme#nt>VP</sales>
<reports-to>1</reports-to>
</person>
<person>
<name>Pip</name>
<id>3</id>
<departme#nt>VP</sales>
<reports-to>1</reports-to>
</person>
</root>



after the transformation it looks like this:

<root name="Pip" id="3" department="VP" reports-to="1">
<person></person>
<person></person>
<person></person>
</root>


I want it to look like this:


<root>
<person name="Johnny" id="1" department="CEO" reports-to="Null"></person>
<person name="Madge" id="2" department="VP" reports-to="1"></person>
<person name="Pip" id="3" department="VP" reports-to="1"></person>
</root>


Your help is greatly appreciated!

thanks.
Sep 23 '08 #1
2 2257
jkmyoung
2,057 Expert 2GB
1. The for loop is a level too high -> You get all the attributes in the first node.
2. Your template matches root first. There's 2 possible fixes

A. Have 1 template for root, 1 for person.
B. Rewrite to use other generic templates.

I like B better, so I'll show the solution for that.

Expand|Select|Wrap|Line Numbers
  1. <xsl:template match="*[*]"> <!-- copy element with children -->
  2.   <xsl:copy>
  3.     <xsl:apply-templates/>
  4.   </xsl:copy>
  5. </xsl:template>
  6. <xsl:template match="*[not (*)]"> 
  7.    <xsl:attribute name="{name()}">
  8.      <xsl:value-of select="."/>
  9.   </xsl:attribute>
  10. </xsl:template>
  11.  
The condition [not(*)] isn't really necessary, but helps avoid confusion.
Sep 23 '08 #2
Many thanks. Great Solution!
Sep 24 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Leif K-Brooks | last post by:
I'm writing a site with mod_python which will have, among other things, forums. I want to allow users to use some HTML (<em>, <strong>, <p>, etc.) on the forums, but I don't want to allow bad...
3
by: Mike | last post by:
Newbie question. I want to traverse an almost arbitrary XML formatted file and punch its attributes into a list. The list will contain the following information: Row-index, indent-level,...
13
by: Dan R Brown | last post by:
I have a large form that is generated dynamically in a jsp using xml / xslt. So, to break up this form into several "tabbed" sections, I break up the form using <div> tags. Each <div...
3
by: Stephan Brunner | last post by:
Hi I have created two flavors of an XSLT stylesheet to transform all attributes of an XML document to elements: They both work as expected with MSXML and XMLSPY but throw an exception ...
2
by: jkflens | last post by:
Hello, i convert one XML-document by using XSLT into another XML-document. First change all attributes to elements is no problem. Then i try to insert a new element into the new document by...
1
by: Foxpointe | last post by:
Given some arbitrary XHTML, I'd like to obtain a 'simplified' XHTML result which strips out a large subset of standard elements and attributes - but not all. The main things I would like to...
1
by: tslettebo | last post by:
Hi all. I've read Michael Kay's "XSLT" book, and used XSLT successfully as an HTML template system at our company (using basically the "fill-in-the-blanks" pattern of XSLT use: A template...
6
by: Jakub.Bednarczuk | last post by:
Hallo everybody I have the problem with getting attributes values and also attributes names. I am reading an xml file with DOM. Lets see an example: file I read <root> <Def></Def>...
5
matheussousuke
by: matheussousuke | last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails. The goal is: Make it send the email with the URL...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.