473,322 Members | 1,431 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,322 software developers and data experts.

Xslt question: how to transform children of a node into siblings?

Hi,
I need some help figuring out how to do something in xslt. I have an xml
document with a list of items. Each item in turn can have a list of
options, like so:

<item>
<stuff>Item 1 Stuff</stuff>
<option>
<option_stuff>Option 1 Stuff</option_stuff>
</option>
<option>
<option_stuff>Option 2 Stuff</option_stuff>
</option>
...
</item>
....

I need to transform that by turning the option tags into item tags, and
making them siblings of their parent item, like so:

<item>
<stuff>Item 1 Stuff</stuff>
</item>
<item>
<stuff>Option 1 Stuff</stuff>
</item>
<item>
<stuff>Option 2 Stuff</stuff>
</item>
....

The ordering needs to be preserved, so that the item1 node comes before
the options, and the item1 options come before the item2 node, etc. How
can I accomplish this with xslt?

thanks,
Gabe
May 1 '06 #1
2 1691
hi,

try the following. I'm inserting an end tag for "item" when I see the first
"option" node and then processing the following "option" nodes.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- for every item node in document-->
<xsl:template match="//item">
<!--for the first stuff node under item-->
<xsl:if test="name((./*)[1])='stuff'">
<xsl:value-of select="'&lt;item&gt;'" disable-output-escaping="yes"/>
<xsl:copy-of select="stuff"/>
<xsl:value-of select="'&lt;/item&gt;'" disable-output-escaping="yes"/>
</xsl:if>
<!--for all the option nodes under item -->
<xsl:for-each select="option">
<xsl:element name="item">
<xsl:element name="stuff">
<xsl:value-of select="option_stuff"/>
</xsl:element>
</xsl:element>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
hope that helps,
swapna

"Gabe Moothart" <ga**@imaginesystems.net> wrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
Hi,
I need some help figuring out how to do something in xslt. I have an xml
document with a list of items. Each item in turn can have a list of
options, like so:

<item>
<stuff>Item 1 Stuff</stuff>
<option>
<option_stuff>Option 1 Stuff</option_stuff>
</option>
<option>
<option_stuff>Option 2 Stuff</option_stuff>
</option>
...
</item>
...

I need to transform that by turning the option tags into item tags, and
making them siblings of their parent item, like so:

<item>
<stuff>Item 1 Stuff</stuff>
</item>
<item>
<stuff>Option 1 Stuff</stuff>
</item>
<item>
<stuff>Option 2 Stuff</stuff>
</item>
...

The ordering needs to be preserved, so that the item1 node comes before
the options, and the item1 options come before the item2 node, etc. How
can I accomplish this with xslt?

thanks,
Gabe

May 1 '06 #2
Gabe Moothart wrote:
I need some help figuring out how to do something in xslt. I have an xml
document with a list of items. Each item in turn can have a list of
options, like so:

<item>
<stuff>Item 1 Stuff</stuff>
<option>
<option_stuff>Option 1 Stuff</option_stuff>
</option>
<option>
<option_stuff>Option 2 Stuff</option_stuff>
</option>
...
</item>
...

I need to transform that by turning the option tags into item tags, and
making them siblings of their parent item, like so:

<item>
<stuff>Item 1 Stuff</stuff>
</item>
<item>
<stuff>Option 1 Stuff</stuff>
</item>
<item>
<stuff>Option 2 Stuff</stuff>
</item>
...

The ordering needs to be preserved, so that the item1 node comes before
the options, and the item1 options come before the item2 node, etc. How
can I accomplish this with xslt?


Something as simple as this:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:apply-templates select="//item"/>
</xsl:template>
<xsl:template match="item">
<item>
<xsl:copy-of select="stuff" />
</item>
<xsl:apply-templates select="option"/>
</xsl:template>
<xsl:template match="option">
<item>
<stuff>
<xsl:value-of select="option_stuff" />
</stuff>
</item>
</xsl:template>
</xsl:stylesheet>
--
Oleg Tkachenko [XML MVP, MCAD]
http://www.XmlLab.Net | http://www.XLinq.Net | http://blog.tkachenko.com
May 2 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Philipp Lenssen | last post by:
I have this XML file which I would like to XSLT-sort based on the string contained within the item-children elements using basic MSXML (no recent version of IIS, so it might be an outdated MSXML --...
5
by: Ruthless | last post by:
hello. All XML and XSLT are processed by preprocessor as a trees. How can i simply display my XML as some kind of tree. given xml: <struct> <node level="1" no="1">
1
by: darrel | last post by:
I know you CAN'T nest templates, so what would be the logic to use otherwise? Here's what I am doing: I have a template set to match a specific node based on one of it's elements. If it...
10
by: Bonj | last post by:
I almost understand TSTs, to the point where I just need to know the answer to this: When making a TST (in C++) that will have as its leaf nodes words that make up SQL language and an categorising...
1
by: arnold | last post by:
Hi, I've been knocking my head against the wall trying to create an XSL transform to perform "normalizations" of a set of XML files that have a common structure. % XML file before transform
18
by: yinglcs | last post by:
Hi, I have a newbie XSLT question. I have the following xml, and I would like to find out the children of feature element in each 'features' element. i.e. for each <featuresI would like to...
8
by: Hercules Dev. | last post by:
Hi all, I'm new in xslt and xpath, so my question might be simple but i'm learning. I have an XML document and need to transform it into another XML, I use xslt and it works, but there is a...
11
by: =?ISO-8859-1?Q?Jean=2DFran=E7ois_Michaud?= | last post by:
Context: I'm trying to compare XML tree fragments and I'm doing so by outputting the attributes of each element in the tree and outputting it to a string then normalizing the strings. Then I'm...
3
by: z1 | last post by:
hi- i am fooling around with soap and weather templates. for some reason either this if or select is failing. i am very new to xml and found this code at another site. i can show you the xml...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.