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

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>paramname0</v0>
<v1>paramname1</v1>
<v2>paramname2</v2>
</field>
<field name="parametervalue">
<v0>paramvalue0</v0>
<v1>paramvalue1</v1>
<v2>paramvalue2</v2>
</field>
<field name="parameterunit">
<v0>paramunit0</v0>
<v1>paramunit1</v1>
<v2>paramunit2</v2>
</field>
<record>

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

<record>
<field name="paramname0" value="paramvalue0" unit="paramunit0"/>
<field name="paramname1" value="paramvalue1" unit="paramunit1"/>
<field name="paramname2" value="paramvalue2" unit="paramunit2"/>
</record>

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

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

<xsl:template match="record/field[@name='parametervalue']"/>
<xsl:template match="record/field[@name='parameterunit']"/>

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 2112
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:stylesheet 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='parameter']">
<xsl:for-each select="*[starts-with(local-name(),'v')]">
<field name="{.}"
value="{key('v',local-name())[../@name='parametervalue']}"
unit="{key('v',local-name())[../@name='parameterunit']}"/>
</xsl:for-each>
</xsl:template>

<xsl:template match="record/field[@name='parametervalue']"/>
<xsl:template match="record/field[@name='parameterunit']"/>

</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='parameter']">
<xsl:for-each select="*[starts-with(local-name(),'v')]">
<field name="{.}"
value="{key('v',local-name())[../@name='parametervalue']}"
unit="{key('v',local-name())[../@name='parameterunit']}"/>
</xsl:for-each>
</xsl:template>

<xsl:template match="record/field[@name='parametervalue']"/>
<xsl:template match="record/field[@name='parameterunit']"/>

[snip]


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

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

<xsl:template match="record/field[@name='parametervalue']"/>
<xsl:template match="record/field[@name='parameterunit']"/>

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
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...
4
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...
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 ...
1
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...
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
2
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...
2
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...
4
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
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
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...
0
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...
0
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 projectplanning, coding, testing,...

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.