Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XSLT: iterating all child elements and accessing homonymous childrenin sibling elements

Reply
Thread Tools

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

 
 
Gerald Aichholzer
Guest
Posts: n/a
 
      06-27-2006
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
 
Reply With Quote
 
 
 
 
Joris Gillis
Guest
Posts: n/a
 
      06-27-2006
On Tue, 27 Jun 2006 11:11:13 +0200, Gerald Aichholzer
<(E-Mail Removed)> 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 xmlnssl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xslutput 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)
«Εν οίδα ότι ουδ*ν οίδα» - Σωκρατης
 
Reply With Quote
 
 
 
 
Gerald Aichholzer
Guest
Posts: n/a
 
      06-27-2006
Hi Joris,

Joris Gillis wrote:
> On Tue, 27 Jun 2006 11:11:13 +0200, Gerald Aichholzer
> <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
treewalker: child-sibling question Ward Germonpr Javascript 7 12-05-2006 08:39 AM
Iterating through all the menu/child items in a menu control Bryan ASP .Net 1 03-01-2006 09:38 AM
how to find not the next sibling but the 2nd sibling or find sibling "a" OR sinbling "b" localpricemaps@gmail.com Python 11 01-23-2006 07:04 PM
Example of sibling and child Petterson Mikael XML 1 08-24-2005 07:39 AM
How do I: Main thread spawn child threads, which child processes...control those child processes? Jeff Rodriguez C Programming 23 12-09-2003 11:06 PM



Advertisments