[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] If/then/else confusion


Subject: Re: [xsl] If/then/else confusion
From: Syd Bauman <Syd_Bauman@xxxxxxxxx>
Date: Tue, 25 Dec 2012 15:00:10 -0500

Can you provide some sample input for which the Prefix='A' is not
working? Are you using XSLT 1 or XSLT 2 (or 3)? It can make a big
difference, here.

I'm not sure I understand the problem thoroughly, but remember that
in XSLT 2 (and I presume in 3)
  Prefix != 'A'
means "is there any member of the sequence of my children <Prefix>
elements whose string value is not 'A'?", not "are all of the members
of the sequence of my children <Prefix> elements something other than
'A'?".

I'm not sure that's very well expressed. Here is an experiment you
can run to prove the point.

input
-----
<?xml version="1.0" encoding="UTF-8"?>
<stuff>
  <sort-us-maybe n="1">
    <Prefix>A</Prefix>
    <Prefix>B</Prefix>
    <Prefix>C</Prefix>
  </sort-us-maybe>
  <sort-us-maybe n="2">
    <Prefix>D</Prefix>
    <Prefix>E</Prefix>
    <Prefix>F</Prefix>
  </sort-us-maybe>
  <sort-us-maybe n="3">
    <Prefix>A</Prefix>
    <Prefix>A</Prefix>
    <Prefix>A</Prefix>
  </sort-us-maybe>
</stuff>

transform
---------
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

  <xsl:output method="text"/>

  <xsl:template match="/stuff">
    <xsl:apply-templates select="sort-us-maybe"/>
    <xsl:text>&#x0A;</xsl:text>
  </xsl:template>

  <xsl:template match="sort-us-maybe">
    <xsl:text>&#x0A;</xsl:text>
    <xsl:value-of select="concat( @n, '--')"/>
    <xsl:text>&#x0A;Prefix=A: </xsl:text>
    <xsl:value-of select="Prefix = 'A'"/>
    <xsl:text>&#x0A;Prefix!=A: </xsl:text>
    <xsl:value-of select="Prefix != 'A'"/>
    <xsl:text>&#x0A;not(Prefix=A): </xsl:text>
    <xsl:value-of select="not( Prefix = 'A' )"/>
    <xsl:text>&#x0A;not(Prefix!=A): </xsl:text>
    <xsl:value-of select="not( Prefix != 'A' )"/>
  </xsl:template>

</xsl:stylesheet>

output
------

1--
Prefix=A: true
Prefix!=A: true
not(Prefix=A): false
not(Prefix!=A): false
2--
Prefix=A: false
Prefix!=A: true
not(Prefix=A): true
not(Prefix!=A): false
3--
Prefix=A: true
Prefix!=A: false
not(Prefix=A): false
not(Prefix!=A): true


> Hello, I am trying to modify a sort-select statement I worked out a
> very long time ago. In the original, I wanted not to sort by a
> <Prefix> if it were an 'A' and this code does just that:
> 
> <xsl:sort select="if( Prefix!='A' ) then Prefix else ''"/>
> 
> I am not sure why its positive version does not work: <xsl:sort
> select="if( Prefix='A' ) then '' else Prefix"/>
> 
> However, my new task is to not sort by (1) <Prefix> if the prefix
> is 'A' or, (2) if the <CatalogName> contains 'SG'.
> 
> In the positive the statement look is:
> 
> <xsl:sort select="if( Prefix='A' or contains(CatalogName, 'SG')) then
> '' else Prefix"/>
> 
> That statement somehow works for the second filter, [the contains],
> but not the first [the prefix='A'].
> 
> I have no idea what is going on and would appreciate any help I can
> get. Thanks.


Current Thread
Keywords