Groups > Design > Microsoft xsl > Re: How to choose the first nonempty element




How to choose the first nonempty element

How to choose the first nonempty element
Wed, 2 Apr 2008 08:29:12 -0700
Hi

If I have an xml data such as the following:

<csv>

<line>
<field></field>
</line>

<line>
<field></field>
</line>

<line>
<field>2</field>
</line>

<line>
<field>3</field>
</line>

</csv>

I need to go though all <line> elements and get the first NON-Empty
<field>
in the example above, it should print 2

it is like using for-each and break with the first non-empty field

I need the solution in xsl 2.0

thanks in advance

Post Reply
Re: How to choose the first nonempty element
Wed, 2 Apr 2008 10:42:43 -0700
On Apr 2, 6:08 pm, Martin Honnen <mahotr...@yahoo.de> wrote:
> khalil...@hotmail.com wrote:
> > If I have an xml data such as the following:
>
> > <csv>
>
> > <line>
> > <field></field>
> > </line>
>
> > <line>
> > <field></field>
> > </line>
>
> > <line>
> > <field>2</field>
> > </line>
>
> > <line>
> > <field>3</field>
> > </line>
>
> > </csv>
>
> > I need to go though all <line> elements and get the first
NON-Empty
> > <field>
> > in the example above, it should print 2
>
>    (/csv/line/field[node()])[1]
> selects the first field element that is not empty.
>
> > I need the solution in xsl 2.0
>
>    <xsl:stylesheet
>      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>      version="2.0">
>
>      <xsl:template match="/">
>        <xsl:value-of
select="(/csv/line/field[node()])[1]"/>
>      </xsl:template>
>
>    </xsl:stylesheet>
>
> That would work with XSLT 1.0 as well.
>
> --
>
>         Martin Honnen --- MVP XML
>        http://JavaScript.FAQTs.com/- Hide quoted text -
>
> - Show quoted text -

Post Reply
Re: How to choose the first nonempty element
Wed, 02 Apr 2008 18:08:26 +020
khalil_mi@hotmail.com wrote:

> If I have an xml data such as the following:
> 
> <csv>
> 
> <line>
> <field></field>
> </line>
> 
> <line>
> <field></field>
> </line>
> 
> <line>
> <field>2</field>
> </line>
> 
> <line>
> <field>3</field>
> </line>
> 
> </csv>
> 
> I need to go though all <line> elements and get the first NON-Empty
> <field>
> in the example above, it should print 2

   (/csv/line/field[node()])[1]
selects the first field element that is not empty.


> I need the solution in xsl 2.0

   <xsl:stylesheet
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     version="2.0">

     <xsl:template match="/">
       <xsl:value-of select="(/csv/line/field[node()])[1]"/>
     </xsl:template>

   </xsl:stylesheet>

That would work with XSLT 1.0 as well.

-- 

	Martin Honnen --- MVP XML
Post Reply
about | contact