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

G

Gerald Aichholzer

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
 
J

Joris Gillis

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:eek:utput 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,
 
G

Gerald Aichholzer

Hi Joris,

Joris said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top