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

Hi,

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
<TEST>
    <FLD1 attr1='attr11' attr2='attr12' attr3='attr13'>
        <CMP num='11' />
        <CMP num='13' />
        <CMP num='12' />
        <CMP num='14' />
    </FLD1>

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

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


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...
   attr11|attr12|attr13|11|12|13|14|NA|
   attr24|attr25|attr26|21|22|23|NA|NA|
   attr04|attr05|attr06|01|02|NA|NA|NA|

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="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>

<xsl:template match="/TEST">

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

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

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

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

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

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

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

         <xsl:text>
</xsl:text>
    </xsl:for-each>

</xsl:template>


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

</xsl:stylesheet>

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...

Rog


---------------------------------------------------------
Insert witty interesting comment here...
Reply With Quote