Re: How Can You Exit an xsl:for-each Loop Prematurely?

Subject: Re: How Can You Exit an xsl:for-each Loop Prematurely?
From: "Dan Machak" <machak@xxxxxxxxxxxx>
Date: Thu, 23 Sep 1999 14:00:09 -0700
Try this:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/XSL/Transform/1.0";>
<xsl:output indent="yes"/>

<xsl:template match="METADATA">
  <FIRST-IN-METADATA>
	<xsl:apply-templates
select="(CODES/CODE[@CODE=//StringMap/textIn/@SupCat])[1]"/>
  </FIRST-IN-METADATA>
</xsl:template>

<xsl:template match="StringMap">
  <FIRST-IN-StringMap>
	<xsl:apply-templates
select="(textIn[@SupCat=//METADATA/CODES/CODE/@CODE])[1]"/>
  </FIRST-IN-StringMap>
</xsl:template>

<xsl:template match="CODE">
  	<xsl:value-of select="@CODE"/>
</xsl:template>

<xsl:template match="textIn">
  	<xsl:value-of select="@SupCat"/>
</xsl:template>

</xsl:stylesheet>

The first template will get the first matched element in METADATA, the second
template will get the first in StringMap.

Dan

On Sep 23,  3:33pm, Michael Teigman wrote:
> Subject: Re: How Can You Exit an xsl:for-each Loop Prematurely?
>
> I was afraid that using the example with a null was going to oversimplify the
> problem.
>
> Here is a portion of my actual XLM which I am supplied and have no control
over.
>
> <METADATA>
>      <CODES CLASS="CATEGORY">
>           <CODE CODE="NEWS"/>
>           <CODE CODE="IT"/>
>           <CODE CODE="DE"/>
>           <CODE CODE="EUROPE"/>
>           <CODE CODE="CRIM"/>
>           <CODE CODE="GB"/>
>           <CODE CODE="ODD"/>
>      </CODES>
> </METADATA>
>
> <StringMap>
>      <textIn SupCat="AAA" iCat="Ratings News"/>
>      <textIn SupCat="ADV" iCat="Advertising"/>
>      <textIn SupCat="ODD" iCat="Human Interest"/>
>      <textIn SupCat="GB" iCat="Great Britain"/>
> </StringMap>
>
> The actual transformation is to see if any CODE attribute in the METADATA
> matches any SupCat in the StringMap and if it does then output the iCat
value.
>
> BUT I want to output only one match, i.e., if there is more than one match
(as
> in this case GB and ODD both match) I want only one of them.
>
> I would also like it to be determinant which would be output given the
ordering
> in the METADATA and/or the StringMap.
>
> I am able to write a transform which gets all of the matches but not only the
> "first".
>
>      mike
>
>
>
>
>
>  (Embedded
>  image moved   Steve Dahl <sdahl@xxxxxxxxxxx>
>  to file:      09/23/99 02:02 PM
>  pic25952.pcx)
>
>
>
>
> To:   Michael Teigman <michael.teigman@xxxxxxxxxxx>
> cc:    (bcc: Michael Teigman/HPG/US/Reuters)
> Subject:  Re: How Can You Exit an xsl:for-each Loop Prematurely?
>
>
>
>
>
>
> Try something like this:
>
> <xsl:for-each select="//Salesman/Orders/@Order[string(.) != ''][1]">
>     ...output something...
> </xsl:for-each/>
>
> The first bracket should eliminate empty attributes, and the second should
> restrict the search to the
> first one only.
>
>
> Michael Teigman wrote:
>
> > My source XML has an element with a set of attributes with the same name
e.g.,
> >
> > <Salesman>
> >      <Orders Order=""/>
> >      <Orders Order=""/>
> >      <Orders Order="zzzzzzzzz"/>
> >      <Orders Order="ee"/>
> > </Salesman>
> >
> > To simplify, I am doing the equivalent of trying to find the first non-null
> > Order, e.g.:
> >
> > <xsl:for-each select="//Salesman/Orders/@Order">
> >      <xsl:variable name="orderID">
> >                     <xsl:value-of select="."/>
> >      </xsl:variable>
> >      <xsl:if test="string-length($orderID) > 0">
> >           ..... output something .....
> >      </xsl:if>
> > </xsl:for-each>
> >
> > I want only the first output. I do not want multiple outputs.
> >
> > How could I exit after the first or suppress the additional outputs after I
> have
> > found one?
> >
> >      mike
>
> --
> - Steve Dahl
> sdahl@xxxxxxxxxxx
>
>
>
>
>
> ------------------------------------------------------------------------
> Any views expressed in this message are those of the individual  sender,
> except  where  the  sender  specifically  states them to be the views of
> Reuters Ltd.
>
> [ Attachment (application/octet-stream): "pic25952.pcx" 3279 bytes
>   Encoded with "base64" ]
>-- End of excerpt from Michael Teigman



-- 
<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
<> Dan Machak                    <>  machak@xxxxxxxxxxxx  <>
<> MS T27A-1                     <>  650-604-2388 (VOICE) <>
<> NASA Ames Research Center     <>  650-604-3957 (FAX)   <>
<> Moffett Field, CA 94035-1000  <>                       <>
<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread