473,569 Members | 2,688 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Trouble converting elements to attributes with XSLT

3 New Member
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:styleshe et 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:attribut e 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>CE O</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 2270
jkmyoung
2,057 Recognized Expert Top Contributor
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
doozer1979
3 New Member
Many thanks. Great Solution!
Sep 24 '08 #3

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

Similar topics

4
2441
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 elements and attributes (onclick, <script>, etc.). I would also like to do basic validation (no overlapping elements like <strong><em>foo</em></strong>,...
3
1079
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, element-name, attribute-name, attribute value. I was almost able to do this in two stages. The first XSLT script punched an intermediate XML file...
13
40656
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 style="display:none"> can be displayed by setting the style attribute to "display:", or hidden with "display:none". This gives the illusion that the person...
3
9906
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 ========================= <?xml version="1.0" encoding="iso-8859-1"?> <xsl:stylesheet version="1.0"
2
2355
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 XSLT, but it doesn't work correctly :-( Example:
1
1262
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 accomplish: 1) Provide a list of elements/attributes to be stripped (i.e. everything else should be passed through) or those that should be passed...
1
1324
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 matching the root node, containing the HTML page, with lots of <xsl:value-of.../>, etc.). However, I've recently got a task that got me stumped, and I...
6
1887
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> <Elements> <Element att1="1" att2="2" att3="3" //Some attributes are
5
13324
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 http://mghospedagem.com/images/controlpanel.jpg instead of http://mghospedagem.comhttp://mghospedagem.com/images/controlpanel.jpg As u see, there's the...
0
7926
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8138
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6287
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5223
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3657
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2117
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1228
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
946
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.