Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 25th, 2010, 06:32 PM
Registered User
 
Join Date: Apr 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default how to exit from for-each

Hi,

By going through some of the previous threads, i got to know that there is no such thing called exit in case of an for-each in xslt as for-each is not really a loop here, but then please could anyone suggest me how to deal with the following scenario:

Sample Input:

<ns0:l>
<ns0:e>has_swatch</ns0:e>
<ns0:e>date_update_curr</ns0:e>
<ns0:e>supplier_cost_currency_curr</ns0:e>
<ns0:e>supplier_returns_cost_curr</ns0:e>
<ns0:e>supplier_fulfillment_cost_curr</ns0:e>
<ns0:e>supplier_unit_cost_curr</ns0:e>
</ns0:l>
<ns0:l>
<ns0:e>Y</ns0:e>
<ns0:e>04-JAN-2010 21:09:10</ns0:e>
<ns0:e>GBP</ns0:e>
<ns0:e>3</ns0:e>
<ns0:e>4</ns0:e>
<ns0:e>5</ns0:e>
</ns0:l>

XSL Logic:
<xsl:for-each select="ancestor-or-self::ns2:doc/ns2:l[$attrname]/ns2:e">
<xsl:if test="(.='supplier_unit_cost_curr' or .='supplier_fulfillment_cost_curr' or .='supplier_returns_cost_curr' or .='supplier_cost_currency_curr')">
<xsl:variable name="temp1" select="position()"/>
<xsl:if test="ancestor-or-self::ns2:doc/ns2:l[$root_pos]/ns2:e[$temp1]!=''">
<Attribute>
<xsl:attribute name="Row">0</xsl:attribute>
<xsl:attribute name="GroupName">supplier_cost_curr</xsl:attribute>
<Name>date_update_curr</Name>
<xsl:element name="Value">
<xsl:call-template name="formatDateTime">
<xsl:with-param name="dateTime" select="$SysDateTime"/>
</xsl:call-template>
</xsl:element>
</Attribute>
<Attribute>
<xsl:attribute name="Row">0</xsl:attribute>
<xsl:attribute name="GroupName">supplier_cost</xsl:attribute>
<Name>date_update</Name>
<xsl:element name="Value">
<xsl:call-template name="formatDateTime">
<xsl:with-param name="dateTime" select="$SysDateTime"/>
</xsl:call-template>
</xsl:element>
</Attribute>
</xsl:if>
</xsl:if>
</xsl:for-each>
Scenario: I have to create the two above mentioned date attributes, only if the input xml contains any/all of the four supplier cost fields and the date fields should get created only once if any/all of the four supplier cost fields are present in the input. I tried the above xsl logic, but it fails if all the four or some of the supplier cost fields are present in the input, the output has repeating date fields coz of the obvious reason behind the logic applied above.

Any pointers/help will be appreciated....

Thanks,
Vijay
 
Old April 26th, 2010, 04:31 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Given that your XSLT code doesn't actually work, it's difficult to use it to reverse engineer your requirements. Apart from anything else, it uses variables like $attname and $root_pos which you haven't explained. So let's ignore it and concentrate on your English statement of requirements:

"I have to create the two above mentioned date attributes, only if the input xml contains any/all of the four supplier cost fields and the date fields should get created only once if any/all of the four supplier cost fields are present in the input. I tried the above xsl logic, but it fails if all the four or some of the supplier cost fields are present in the input, the output has repeating date fields coz of the obvious reason behind the logic applied above."

Now, I'm not sure what the "two above mentioned date attributes are", and I don't know what "any/all" is supposed to mean - does it mean "any", or "all" - it can't mean both! However, one thing is clear, and that's that you don't need a for-each in the solution. You want something much closer to the logic of your requirements statement: if (the relevant fields exist) then (create the required attributes)".

Showing your required output might be useful.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old April 26th, 2010, 06:22 AM
Registered User
 
Join Date: Apr 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Michael,

Thanks for your reply...

The date attributes are the once which are mentioned in the xsl logic:'date_update_curr' and 'date_update' and 'any/all' means both can exist. You are very right that the logic should be like if (supplier cost fields exist) then (create 'date_update_curr' and 'date_update'), but also we have to go through each node of the input xml to check the existence of the supplier cost fields, so my concern here is that how we can check each node and implement the if-then condition as well.

The required output should be like:
<Attribute Row="0" GroupName="supplier_cost">
<Name>date_update</Name>
<Value>19-APR-2010 18:45:26</Value>
</Attribute>
<Attribute Row="0" GroupName="supplier_cost_curr">
<Name>date_update_curr</Name>
<Value>19-APR-2010 18:45:26</Value>
</Attribute>

Thanks,
Vijay
 
Old April 26th, 2010, 07:33 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Thumbs up

Not sure what $attrname and $root_pos are, but with some assumption I coded the below. I have added <root> as root element to your input xml, deleted the prefixes and modified your code.
Code:
<xsl:template match="root">
<xsl:apply-templates/>
</xsl:template> 
<xsl:template match="doc">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="l">
<xsl:choose>
<xsl:when test="e[(.='supplier_unit_cost_curr' or .='supplier_fulfillment_cost_curr' or .='supplier_returns_cost_curr' or .='supplier_cost_currency_curr')]">
<xsl:variable name="temp1" select="position()"/>
<xsl:if test="ancestor-or-self::doc/l/e[$temp1]!=''">
<Attribute>
<xsl:attribute name="Row">0</xsl:attribute>
<xsl:attribute name="GroupName">supplier_cost_curr</xsl:attribute>
<Name>date_update_curr</Name>
<xsl:element name="Value">
<!--
<xsl:call-template name="formatDateTime">
<xsl:with-param name="dateTime" select="$SysDateTime"/>
</xsl:call-template>
-->
</xsl:element>
</Attribute>
<Attribute>
<xsl:attribute name="Row">0</xsl:attribute>
<xsl:attribute name="GroupName">supplier_cost</xsl:attribute>
<Name>date_update</Name>
<xsl:element name="Value">
<!--
<xsl:call-template name="formatDateTime">
<xsl:with-param name="dateTime" select="$SysDateTime"/>
</xsl:call-template>
-->
</xsl:element>
</Attribute>
</xsl:if> 
</xsl:when>
</xsl:choose>
</xsl:template>
Check if the above is okay.
__________________
Rummy





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to exit from loop dishant XSLT 2 October 6th, 2008 03:34 AM
on exit mohiddin52 Access VBA 2 July 10th, 2007 09:28 AM
how to exit from function try.test.abc C# 2 January 11th, 2007 06:24 AM
exit from for-each preethi.s XSLT 1 September 8th, 2006 05:37 AM
session_destroy on exit?? harmen_wessels PHP How-To 2 July 5th, 2003 09:41 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.