RE: Re: [xsl] How to do an 'existence' test in XSL?

Subject: RE: Re: [xsl] How to do an 'existence' test in XSL?
From: George James <GeorgeJ@xxxxxxxxxxxxxxx>
Date: Thu, 23 Dec 2004 12:43:51 -0000
Ben
My solution makes the assumption that there is a known fixed list of gui
types.  If your gui tags are scattered anywhere in the document then the
following variation would work:

  <!-- Match the document root -->
  <xsl:template match="/">
    <root>

      <!-- If the gui element appears anywhere in the document and matches
the
           appropriate type then generate one instance of the relevant
output
           for that type -->
      <xsl:if test=" //gui[@type='alertBox']  ">
        <alertBox/>
      </xsl:if>

      <xsl:if test=" //gui[@type='tooltip']  ">
        <tooltip/>
      </xsl:if>

      <xsl:if test=" //gui[@type='help']  ">
        <help/>
      </xsl:if>

    </root>
  </xsl:template>

The // syntax here means match on any gui element anywhere below the current
context node (which in this case is / which is the document root) - I think
of it as kind of like a "dir gui* /s" at a windows command prompt.

Performance wise, this will probably scan the whole document once for each
instance of the <xsl:if> condition.  If you have lots of gui types and/or a
very large document then you might notice the speed, otherwise it would be
fine.

One other consideration is whether the output will be substantially the same
for each gui type or different.  If it is different then you can just paste
the appropriate output into the body of each of the <xsl:if>'s above.  I
don't think it would be so easy to do that for some of the more generalised
solutions that have been posted.

Regards
George

George James Software
www.georgejames.com
An InterSystems Technology Partner





> -----Original Message-----
> From: ben@xxxxxxxxxxxxx [mailto:ben@xxxxxxxxxxxxx]
> Sent: 23 December 2004 11:38
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: Re: Re: [xsl] How to do an 'existence' test in XSL?
>
>
>
> George certainly has a point... computer languages are
> modelling languages. If they are not able to elegantly model
> a common phenomenon (which this is) then they aren't doing
> their job too well, doubtless why XSL 2.0 has this new property.
>
> However, the scenario I outlined was overly simplistic.
>
> +It should not be assumed that the gui tags are are siblings.
> They may
> +be scattered throughout the document at various nestings+
>
> One of my additional problems is that I am using PHP5 and
> thus James Clark's expat parser, which doesn't support keys?
>
> In this case I wonder which of the various proposed
> techniques will work!
>
> Many thanks for all the input, the discussion certainly
> highlights many potentially useful approaches for an XSL
> beginner such as myself!
>
> Ben
>
> P.S. If Dimtre fancies explaining in a little more detail
> what on earth is going on in his script below that would be a
> wonderful thing!
>
>
> You wrote:
> > In case you know all possible types in advance, a simple (but not
> > too-efficient) way of picking up all "existing" gui types is the
> > following:
> >
> > <xsl:stylesheet version="2.0"
> > xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
> >  xmlns:xs="http://www.w3.org/2001/XMLSchema";
> >  >
> >
> >  <xsl:output method="text"/>
> >
> >  <xsl:param name="pAllStyles" as="xs:string+"
> >  select="'alertBox', 'combo', 'help', 'tooltip'"/>
> >
> >  <xsl:variable name="vDoc" select="/"/>
> >
> >   <xsl:template match="/">
> >     <xsl:value-of separator="&#xA;" select=
> >     "$pAllStyles[. = $vDoc/styles/gui/@type ]" />
> >   </xsl:template>
> > </xsl:stylesheet>
> >
> > When this transformation is applied on your source xml
> (provided with
> > a single element parent):
> >
> > <styles>
> >   <gui type="alertBox"/>
> >   <gui type="tooltip"/>
> >   <gui type="help"/>
> >   <gui type="tooltip"/>
> >   <gui type="alertBox"/>
> >   <gui type="tooltip"/>
> >   <gui type="help"/>
> > </styles>
> >
> > the wanted result is produced:
> >
> > alertBox
> > help
> > tooltip
> >
> >
> > Cheers,
> > Dimitre.
> >
> >
> >
> > On Wed, 22 Dec 2004 14:33:06 +0000, ben@xxxxxxxxxxxxx
> > <ben@xxxxxxxxxxxxx> wrote:
> > > I'm having great difficulty understanding how/if XSL provides the
> > > tool to satisfy the following simple requirement.
> > >
> > > Lets say I have some simple xml like :
> > >
> > > <gui type="alertBox">...</gui>
> > > <gui type="tooltip">...</gui>
> > > <gui type="help">...</gui>
> > > <gui type="tooltip">...</gui>
> > > <gui type="alertBox">...</gui>
> > > <gui type="tooltip">...</gui>
> > > <gui type="help">...</gui>
> > >
> > > To simplify things... imagine transforming this document
> in such a
> > > way that we have something like :
> > >
> > > <alertBox/>
> > > <tooltip/>
> > > <help/>
> > >
> > > i.e. I would like the XSL to result in one output per gui type.
> > >
> > > So there is the problem... how on earth do I process the xml such
> > > that it results in an output per +type+ rather than for each
> > > instance (is that explained well enough?)... i.e. it's
> easy to match
> > > on the attributes but each match produces output so I would get :
> > >
> > > <alertBox/><alertBox/>
> > > <tooltip/><tooltip/><tooltip/>
> > > <help/><help/>
> > >
> > > Can anyone offer advice on the way in which I ought to
> approach this
> > > problem?
> > >
> > > Kindest regards,
> > >
> > > Ben
>
> --
> No virus found in this incoming message.
> Checked by AVG Anti-Virus.
> Version: 7.0.296 / Virus Database: 265.6.4 - Release Date: 22/12/2004
>
>

--
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.296 / Virus Database: 265.6.4 - Release Date: 22/12/2004

Current Thread