473,789 Members | 2,544 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XSLT: iterating all child elements and accessing homonymous childrenin sibling elements

Hello NG,

I have the following XML (simplified) having a variable
number of v-elements:

<record>
<field name="parameter ">
<v0>paramname 0</v0>
<v1>paramname 1</v1>
<v2>paramname 2</v2>
</field>
<field name="parameter value">
<v0>paramvalue0 </v0>
<v1>paramvalue1 </v1>
<v2>paramvalue2 </v2>
</field>
<field name="parameter unit">
<v0>paramunit 0</v0>
<v1>paramunit 1</v1>
<v2>paramunit 2</v2>
</field>
<record>

.... and would like to create the following target XML:

<record>
<field name="paramname 0" value="paramval ue0" unit="paramunit 0"/>
<field name="paramname 1" value="paramval ue1" unit="paramunit 1"/>
<field name="paramname 2" value="paramval ue2" unit="paramunit 2"/>
</record>

I'm working with an identity transformation and the following
templates:

<xsl:template match="record/field[@name='paramete r']">
<!-- (*) -->
</xsl:template>

<xsl:template match="record/field[@name='paramete rvalue']"/>
<xsl:template match="record/field[@name='paramete runit']"/>

My problem is the place marked with (*):

How can I iterate over all v elements and access the homo-
nymous child in the elements for the parameter values and
units?

thanx in advance and kind regards,
Gerald
Jun 27 '06 #1
2 2134
On Tue, 27 Jun 2006 11:11:13 +0200, Gerald Aichholzer
<ge************ ***@gmx.net> wrote:
How can I iterate over all v elements and access the homo-
nymous child in the elements for the parameter values and
units?


As simple Xpath query might do the trick, but - for performance - using a
key sounds a beter idea. Example:

<?xml version='1.0' encoding='utf-8' ?>
<xsl:styleshe et xmlns:xsl="http ://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" indent="yes"/>

<xsl:key name="v" match="record/field/*[starts-with(local-name(),'v')]"
use="local-name()"/>

<xsl:template match="record/field[@name='paramete r']">
<xsl:for-each select="*[starts-with(local-name(),'v')]">
<field name="{.}"
value="{key('v' ,local-name())[../@name='paramete rvalue']}"
unit="{key('v', local-name())[../@name='paramete runit']}"/>
</xsl:for-each>
</xsl:template>

<xsl:template match="record/field[@name='paramete rvalue']"/>
<xsl:template match="record/field[@name='paramete runit']"/>

</xsl:stylesheet>

regards,
--
Joris Gillis (http://users.telenet.be/root-jg/me.html)
«Εν οίδα ότι ουδ*ν οίδα» - Σωκρατη
Jun 27 '06 #2
Hi Joris,

Joris Gillis wrote:
On Tue, 27 Jun 2006 11:11:13 +0200, Gerald Aichholzer
<ge************ ***@gmx.net> wrote:
How can I iterate over all v elements and access the homo-
nymous child in the elements for the parameter values and
units?


As simple Xpath query might do the trick, but - for performance - using
a key sounds a beter idea. Example:

[snip]

<xsl:key name="v" match="record/field/*[starts-with(local-name(),'v')]"
use="local-name()"/>

<xsl:template match="record/field[@name='paramete r']">
<xsl:for-each select="*[starts-with(local-name(),'v')]">
<field name="{.}"
value="{key('v' ,local-name())[../@name='paramete rvalue']}"
unit="{key('v', local-name())[../@name='paramete runit']}"/>
</xsl:for-each>
</xsl:template>

<xsl:template match="record/field[@name='paramete rvalue']"/>
<xsl:template match="record/field[@name='paramete runit']"/>

[snip]


thanx a lot for your solution. In the meantime I have come up
against the following solution:

<xsl:template match="record/field[@name='paramete r']">
<xsl:for-each select="./*">
<xsl:variable name="v" select="local-name()"/>
<field name="{.}"
value="{../../field[@name='paramete rvalue']/*[local-name()=$v]}"
unit="{../../Field[@name='paramete runit']/*[local-name()=$v]}"/>
</xsl:for-each>
</xsl:template>

<xsl:template match="record/field[@name='paramete rvalue']"/>
<xsl:template match="record/field[@name='paramete runit']"/>

What I should do is changing the for-each's select to your
solution.

kind regards,
Gerald
Jun 27 '06 #3

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

Similar topics

2
3193
by: Peter Gerstbach | last post by:
Hello, I need help on the new xsl:document element! I'm using XSLT version 1.1 to be able to use the <xsl:document> element, because I need more than 1 output files. I'm using Saxon 6.5.3 My problem is the following. My XML document is a OpenOffice-document. There are headers and text underneath. But unfortunately the text elements are not nested in the header elements. See this simple example:
4
3804
by: Richard Rudie | last post by:
Have any other Windows iTunes users looked at the XML file iTunes uses as its database? (Does iTunes for Mac use an XML file, too?) When I noticed that it was XML, I thought it might be useful, or at least instructional, to create an XSLT file or few to filter and format the database. So I opened it in a text editor, and found a bunch of poorly-designed XML structure---or so it seems to me, as I wouldn't be posting a question otherwise....
3
9927
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"
1
1222
by: Juho Jussila | last post by:
Hi How can I easily ensure that Xml document has elements in certain order? I think it can be done with Xml schema, but I'd like to use Xslt, because the validation is a part of Xslt transformations. Xml document: <Root> <Foo/> <Bar/>
1
2042
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
2
1269
by: Jeff Calico | last post by:
Hello everyone I have sucessfully implemented a SAX filter to take a large XML data file, and strip it down to its essential elements, and then pass that on to an XSL transformer. (A bare bones example of the java code is listed at the end) I also need another XML file, a list of all possible records, which I use for getting additional information by cross referencing with xsl:key. The list file only has one type of element, and I...
2
22780
jkmyoung
by: jkmyoung | last post by:
Here's a short list of useful xslt general tricks that aren't taught at w3schools. Attribute Value Template Official W3C explanation and example This is when you want to put dynamic values in the attribute of an element. Instead of using the <xsl:attribute> element, you can simply place the xpath in the attribute itself. The most common usage of this is in creating hyperlinks.
4
1952
by: MRe | last post by:
Hi, Is it possible using XSLT to transform this.. <test> <b>0</b> <a>1</a> <a>2</a> <b>3</b> <b>4</b>
0
9663
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9511
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10136
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9979
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6765
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5548
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4090
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
2
3695
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2906
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.