XML Editor
Sign up for a WebBoard account Sign Up Keyword Search Search More Options... Options
Chat Rooms Chat Help Help News News Log in to WebBoard Log in Not Logged in
Show tree view Topic
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Postnext
Peter EideSubject: Newbie call-template question
Author: Peter Eide
Date: 11 Feb 2007 06:49 PM
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>

Any help is appreciated. Thanks

Postnext
(Deleted User) Subject: Newbie call-template question
Author: (Deleted User)
Date: 12 Feb 2007 03:31 AM
Hi Peter,
if what you want is just to generate the "characteristic" element only if the "picture" attribute is present, you can use the following:

<xsl:if test="@picture">
...
</xsl:if>

instead of the entire

<xsl:for-each select="./item">
<xsl:choose>
<xsl:when test="@picture">
...
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="foo">
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>

Hope this helps,
Alberto

Postnext
Peter EideSubject: Newbie call-template question
Author: Peter Eide
Date: 12 Feb 2007 12:14 PM
The problem with this is it ignores sublevels if the picture attribute is not present.

Postnext
(Deleted User) Subject: Newbie call-template question
Author: (Deleted User)
Date: 14 Feb 2007 11:49 AM
Oh, so you really meant to look for "item" under "copybook/item"?
In this case you can do

<xsl:for-each select="./item[@picture]">

to create one sub-element for each sub-item that has a picture attribute.

Is this what you had in mind?
Alberto

Posttop
Peter EideSubject: Newbie call-template question
Author: Peter Eide
Date: 14 Feb 2007 05:16 PM
I think I got it. Thanks for the help.

 
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Download A Free Trial of Stylus Studio 6 XML Professional Edition Today! Powered by Stylus Studio, the world's leading XML IDE for XML, XSLT, XQuery, XML Schema, DTD, XPath, WSDL, XHTML, SQL/XML, and XML Mapping!  
go

Log In Options

Site Map | Privacy Policy | Terms of Use | Trademarks
Stylus Scoop XML Newsletter:
W3C Member
Stylus Studio® and DataDirect XQuery ™are from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2016 All Rights Reserved.