View Single Post
  #1 (permalink)  
Old July 5th, 2007, 02:42 AM
tall rog tall rog is offline
Registered User
Join Date: Jul 2007
Location: Wroclaw, , Poland.
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to tall rog
Default Position of sorted nodes


I am using Saxon 8 on XP.

I would like to test the existence of ordered nodes and output either the value of an attribute on the ordered node or a constant value if the node does not exist.

For testing I have the following XML
    <FLD1 attr1='attr11' attr2='attr12' attr3='attr13'>
        <CMP num='11' />
        <CMP num='13' />
        <CMP num='12' />
        <CMP num='14' />

    <FLD1 attr1='attr24' attr2='attr25' attr3='attr26'>
        <CMP num='21' />
        <CMP num='23' />
        <CMP num='22' />

    <FLD1 attr1='attr04' attr2='attr05' attr3='attr06'>
        <CMP num='02' />
        <CMP num='01' />

So we have a number of FLD1 tags which have a number of CMP tags each with a 'num' attribute. There can be anything up to 5 CMP nodes for each FLD1 node

I need to convert this to a pipe delimited format as follows...

So if a Node does not exist then the field should contain 'NA'.

Note that the data from the CMP nodes are sorted by the 'num' attribute. There is no need to sort the FLD nodes.

The closest I have come is using the XSL below, but this does not order the CMP nodes that are there. What I think I need to do is sort the nodes and then test for their existence using the "displayCmp" template I have created. But I can see no way of doing that.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="">
<xsl:output method="text"/>

<xsl:template match="/TEST">

    <xsl:for-each select="./FLD1">

        <xsl:value-of select="@attr1"/>
        <xsl:value-of select="@attr2"/>
        <xsl:value-of select="@attr3"/>

                <xsl:call-template name="displayCmp">
                    <xsl:with-param name="component" select="'1'"/>

                <xsl:call-template name="displayCmp">
                    <xsl:with-param name="component" select="'2'"/>

                <xsl:call-template name="displayCmp">
                    <xsl:with-param name="component" select="'3'"/>

                <xsl:call-template name="displayCmp">
                    <xsl:with-param name="component" select="'4'"/>

                <xsl:call-template name="displayCmp">
                    <xsl:with-param name="component" select="'5'"/>



<xsl:template name="displayCmp">
        <xsl:param name="component"/>
        <xsl:when test="CMP[position()=$component]">
            <xsl:value-of select="CMP[position()=$component]/@num"/>


This is the first bit of XSL I have tried to write, so please excuse me if I am asking about something obvious or if I have used the wrong terminology...


Insert witty interesting comment here...
Reply With Quote