Page 1 of 1

Newbie call-template question

Posted: Mon Feb 12, 2007 8:39 pm
by peteeide
I am trying to loop some code with a simple call-template but cannot get it to work. The input XML is from a copybook of the format:

- <copybook filename="foo.txt">
- <item level="05" name="field1 position="1" display-length="2792" storage-length="2792">
<item level="10" name="field3" picture="X(09)" display-length="9" storage-length="9" position="10" redefined="true" />
- <item level="10" name="FILLER" redefines="field3" position="10" display-length="9" storage-length="9">
<item level="15" name="field4" picture="X(04)" display-length="4" storage-length="4" position="10" />
</item>
- <item level="10" name=field7" position="238" display-length="73" storage-length="73">
<item level="15" name="field8" picture="9(03)" display-length="3" storage-length="3" numeric="true" position="238" />
<item level="15" name="field10" picture="X(06)" display-length="6" storage-length="6" position="249" />
</item>
- <item level="10" name="field11" position="311" display-length="734" storage-length="734">
<item level="15" name="field12" picture="9(04)" display-length="4" storage-length="4" numeric="true" position="311" />
- <item level="15" name="field14" occurs="00010" position="319" display-length="68" storage-length="68">
<item level="20" name="field16" picture="S9(04)" signed="true" display-length="4" storage-length="4" numeric="true" position="339" />
- <item level="20" name="field17" occurs="00004" position="343" display-length="11" storage-length="11">
<item level="25" name="field18" picture="X(02)" display-length="2" storage-length="2" position="348" />
</item>
</item>
</copybook>

I deleted some records so some of the positions are off but the idea remains that same. The basic idea is I need to read each line and test if it has a picture tag. If it does output it in a different format, otherwise ignore and loop back. The main code I have is:

<xsl:for-each select="copybook/item">
<data_source>
<xsl:attribute name="name">
<xsl:value-of select= "./@name"/>
</xsl:attribute>
<xsl:attribute name="type">
<xsl:value-of select= "'Variable'"/>
</xsl:attribute>
<xsl:attribute name="version">
<xsl:value-of select= "'1.0'"/>
</xsl:attribute>
<xsl:variable name="DataSource">
<xsl:value-of select="concat(@name, '.')"/>
</xsl:variable>
<xsl:template name="foo">
<xsl:for-each select="./item">
<xsl:choose>
<xsl:when test="@picture">
<xsl:element name="characteristic">
<xsl:for-each select= "@name">
<xsl:attribute name="{name()}">
<xsl:value-of select="concat($DataSource,.)"/>
</xsl:attribute>
</xsl:for-each>
<xsl:attribute name="version">
<xsl:value-of select= "'1.0'"/>
</xsl:attribute>
<xsl:element name="data_type">
<xsl:choose>
<xsl:when test="@numeric='true'">
<xsl:value-of select="'1'"/>
</xsl:when>
<xsl:when test="@picture != 'true'">
<xsl:value-of select="'2'"/>
</xsl:when>
</xsl:choose>
</xsl:element>
<xsl:element name="length">
<xsl:value-of select= "@display-length"/>
</xsl:element>
<xsl:element name="precision">
<xsl:value-of select= "'0'"/>
</xsl:element>
<xsl:element name="array_size">
<xsl:value-of select= "'0'"/>
</xsl:element>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="foo">
</xsl:call-template>

</xsl:otherwise>
</xsl:template>
</xsl:choose>
</xsl:for-each>
</data_source>
</xsl:for-each>

I tried using just a <xsl:if test="@picture"> ... </xsl:if> statement but the problem with this is it ignores sublevels if the picture attribute is not present.

Any help is appreciated. Thanks