Re: [xsl] If/then/else confusion

Subject: Re: [xsl] If/then/else confusion
From: Mark Wilson <mark@xxxxxxxxxxxx>
Date: Tue, 25 Dec 2012 13:14:26 -0700
Thanks for the info Syd.

I am using XSLT2, which I should have said in the original posting. I'll look at what you have done.

Here is the stylesheet code:
<xsl:template match="List">
<List>
<xsl:for-each-group select="Item" group-by="concat(CatalogName, Prefix, CatalogNumber, Range)">
<!-- <xsl:sort select="CatalogName" data-type="text" collation="{$sorting-collation}"/> -->
<xsl:sort select="if( Prefix!='A' ) then Prefix else ''"/>
<xsl:sort select="CatalogNumber" data-type="number"/>
<Item>
<xsl:copy-of select="CatalogName" copy-namespaces="no"/>
<xsl:copy-of select="Prefix" copy-namespaces="no"/>
<xsl:copy-of select="CatalogNumber" copy-namespaces="no"/>
<xsl:copy-of select="Range" copy-namespaces="no"/>
<xsl:for-each-group select="current-group()" group-by="Article/Title">
<xsl:sort select="Article/Title" data-type="text"/>
<xsl:sort select="Article/Year" data-type="text"/>
<xsl:sort select="Article/IssueNumber" data-type="number"/>
<xsl:copy-of select="current-group()/Article" copy-namespaces="no"/>
</xsl:for-each-group>
</Item>
</xsl:for-each-group>
</List>
</xsl:template>


Here is a short bit of input:
<List>
<Item>
<CatalogName>POFIS-Deskoslovensko</CatalogName>
<CatalogNumber>2896</CatalogNumber>
<Prefix>A</Prefix>
<Article>
<Title>New issues</Title>
<WholeNumber>56</WholeNumber>
<IssueName>Autumn</IssueName>
<Year>1989</Year>
<Page>76</Page>
</Article>
</Item>
<Item>
<CatalogName>SG-Czechoslovakia</CatalogName>
<CatalogNumber>2979</CatalogNumber>
<Prefix>MS</Prefix>
<Article>
<Title>New issues</Title>
<WholeNumber>56</WholeNumber>
<IssueName>Autumn</IssueName>
<Year>1989</Year>
<Page>76</Page>
</Article>
</Item>
<Item>
<CatalogName>POFIS-Deskoslovensko</CatalogName>
<CatalogNumber>2897</CatalogNumber>
<Article>
<Title>New issues</Title>
<WholeNumber>56</WholeNumber>
<IssueName>Autumn</IssueName>
<Year>1989</Year>
<Page>76</Page>
</Article>
</Item>
<Item>
<CatalogName>SG-Czechoslovakia</CatalogName>
<CatalogNumber>2980</CatalogNumber>
<Article>
<Title>New issues</Title>
<WholeNumber>56</WholeNumber>
<IssueName>Autumn</IssueName>
<Year>1989</Year>
<Page>76</Page>
</Article>
</Item>
</List>

On 12/25/2012 1:00 PM, Syd Bauman wrote:
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.





-- Mark Wilson Knihtisk Publishing

Current Thread