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

Discussion in 'XML' started by Gerald Aichholzer, Jun 27, 2006.

  1. 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
    Gerald Aichholzer, Jun 27, 2006
    #1
    1. Advertising

  2. Gerald Aichholzer

    Joris Gillis Guest

    Re: XSLT: iterating all child elements and accessing homonymous children in sibling elements

    On Tue, 27 Jun 2006 11:11:13 +0200, Gerald Aichholzer
    <> 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: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,
    --
    Joris Gillis (http://users.telenet.be/root-jg/me.html)
    «Εν οίδα ότι ουδέν οίδα» - ΣωκÏατης
    Joris Gillis, Jun 27, 2006
    #2
    1. Advertising

  3. Hi Joris,

    Joris Gillis wrote:
    > On Tue, 27 Jun 2006 11:11:13 +0200, Gerald Aichholzer
    > <> 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
    Gerald Aichholzer, Jun 27, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bryan
    Replies:
    1
    Views:
    771
    Steven Cheng[MSFT]
    Mar 1, 2006
  2. Phoenix
    Replies:
    3
    Views:
    1,312
    Joris Gillis
    Dec 30, 2004
  3. Petterson Mikael

    Example of sibling and child

    Petterson Mikael, Aug 24, 2005, in forum: XML
    Replies:
    1
    Views:
    391
    David Dorward
    Aug 24, 2005
  4. Replies:
    11
    Views:
    649
    Fredrik Lundh
    Jan 23, 2006
  5. Ward Germonpré

    treewalker: child-sibling question

    Ward Germonpré, Dec 4, 2006, in forum: Javascript
    Replies:
    7
    Views:
    109
    Ward Germonpré
    Dec 5, 2006
Loading...

Share This Page