Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 7th, 2008, 02:26 PM
Registered User
 
Join Date: Jan 2008
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Sort unrelated strings

I'm a little new to xsl and I am try to sort some unrelated strings.

I'm using a lookup table to and populating with some static data and some variables.

This is an example of what I have tried:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:map="http://test.com/xslt/maps">
    <xsl:template match="/">
                <xsl:variable name="johnBic1">mfil</xsl:variable>
              <xsl:variable name="johnBic2">gsil</xsl:variable>
                <map:bics>
            <map:bic abbrev="bic3">janurary</map:bic>
            <map:bic abbrev="bic4">Februrary</map:bic>
                        <map:bic abbrev="bic5"><xsl:value-of select="$johnBic1"></xsl:value-of></map:bic>
            <map:bic abbrev="bic6"><xsl:value-of select="$johnBic2"></xsl:value-of></map:bic>
            </map:bics>


        <xsl:for-each select="document('client.deal.swift.320.TestMonthM ap')/*/map:bics/map:bic">
            <xsl:sort select="." data-type="text" order="ascending"/>
            <xsl:value-of select="."></xsl:value-of>
        </xsl:for-each>

    </xsl:template>
</xsl:stylesheet>

This does not sor=t the values correctly.

When I declare the map outside the template I can get it to sort correctly. However, it ignores the variables I have declared. Why does this happen?

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:map="http://test.com/xslt/maps">
  <xsl:variable name="johnBic1">mfil</xsl:variable>
              <xsl:variable name="johnBic2">gsil</xsl:variable>
                <map:bics>
            <map:bic abbrev="bic3">janurary</map:bic>
            <map:bic abbrev="bic4">Februrary</map:bic>
                        <map:bic abbrev="bic5"><xsl:value-of select="$johnBic1"></xsl:value-of></map:bic>
            <map:bic abbrev="bic6"><xsl:value-of select="$johnBic2"></xsl:value-of></map:bic>
            </map:bics>

    <xsl:template match="/">


        <xsl:for-each select="document('')/*/map:bics/map:bic">
            <xsl:sort select="." data-type="text" order="ascending"/>
            <xsl:value-of select="."></xsl:value-of>
        </xsl:for-each>

    </xsl:template>
</xsl:stylesheet>

Thanks for your help in advance.


 
Old January 7th, 2008, 02:42 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

<map:bics>
...
</map:bics>

<xsl:for-each select="document('client.deal.swift.320.TestMonthM ap')/*/map:bics/map:bic">

I've no idea what's in that document you are reading, but it certainly isn't the map:bics element you have just created. The output of your stylesheet isn't available to be read until the stylesheet is finished (and if you try and read it before then, anything could happen.)

In your second example, <map:bics> is a "user data element". This is completely passive content, there is no attempt to look inside it to see if it contains any instructions. I would expect that when you read it, it's exactly as if you read an external data file - if it contains XSLT instructions, you will simply read those instructions, you won't execute them.

So, take a step back, and try and explain what problem you are trying to solve. Then we can help suggest the right way to go about it.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old January 7th, 2008, 03:09 PM
Registered User
 
Join Date: Jan 2008
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok,

Sorry for the brief explanation. Its been a long day...

Idealy I want to sort some variables that will be passed in to the stylesheet. We are using formula's to generate these values.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:map="http://test.com/xslt/maps">

    <xsl:param name="ourBic"/>
        <xsl:param name="cntBic"/>

        <map:bics>
            <map:bic abbrev="bic1"><xsl:value-of select="$ourBic"></xsl:value-of></map:bic>
            <map:bic abbrev="bic2"><xsl:value-of select="$cntBic"></xsl:value-of></map:bic>
        </map:bics>

<xsl:template match="/">
        <xsl:for-each select="document('client.deal.swift.320.TestMonthM apOrig')/*/map:bics/map:bic">
            <xsl:sort select="." order="ascending"/>
            <xsl:value-of select="."></xsl:value-of>
        </xsl:for-each>
    </xsl:template>

The idea being that we can pass in two strings to this stylesheet and it will return them in order. client.deal.swift.320.TestMonthMapOrig is referring to itself.
On docs I have read it suggested using document('')/*/map:bics/map:bic as this refers to the current stylesheet.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:map="http://test.com/xslt/maps">

    <map:bics>
            <map:bic abbrev="bic3">MFIL</map:bic>
            <map:bic abbrev="bic4">GSIL</map:bic>
        </map:bics>

    <xsl:template match="/">

        <xsl:for-each select="document('client.deal.swift.320.TestMonthM ap')/*/map:bics/map:bic">
            <xsl:sort select="." data-type="text" order="ascending"/>
            <xsl:value-of select="."></xsl:value-of>
        </xsl:for-each>

    </xsl:template>
</xsl:stylesheet>

The stylesheet above returns "GSILMFIL" I want to be able to do this with dynamic variables not static values.


Thank you
 
Old January 7th, 2008, 04:35 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The limitations of XSLT 1.0 are really frustrating - the only thing you can sort is a node-set; there is no way of creating a node-set and sorting it in a single transformation without the xx:node-set() extension; and there is no way of comparing two strings except by sorting them.

Is there any reason you have to use XSLT 1.0?

In 2.0 it becomes really easy:

<xsl:for-each select="$ourBic, $cntBic">
  <xsl:sort select="."/>
  ...
</xsl:for-each>

Or if you prefer:

<xsl:for-each select="min(($ourBic, $cntBic)), max(($ourBic, $cntBic))">
  ...
</xsl:for-each>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old January 7th, 2008, 05:36 PM
Registered User
 
Join Date: Jan 2008
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The app I am using only supports 1.0. I will look further into xx:node-set().

Thanks for your help this far.

 
Old January 8th, 2008, 06:17 AM
Registered User
 
Join Date: Jan 2008
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry, how do you do this with the saxon extensions?

 
Old January 8th, 2008, 06:24 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

> Sorry, how do you do this with the saxon extensions?

Do you mean with Saxon 6.5.x, which implements XSLT 1.0? (I'm not sure why anyone would be using that in preference to Saxon 9.0, unless you've got some strange constraint like being tied to Java 1.1!)

Like most XSLT 1.0 processors Saxon 6.5.x has a saxon:node-set() extension, which allows you to do

<xsl:variable name="temp">
  <a><xsl:value-of select="$var1"/></a>
  <a><xsl:value-of select="$var2"/></a>
</xsl:variable>

<xsl:for-each select="saxon:node-set($temp)/a">
  <xsl:sort select="."/>
  ....

Or in fact you don't need the saxon:node-set($temp), you can just write select="$temp", provided you say version="1.1" on the xsl:stylesheet element.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old January 8th, 2008, 07:49 AM
Registered User
 
Join Date: Jan 2008
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Michael. This is working for me in Oxygen but not in the application I am using. Think its time to contact them.

Thanks again






Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic sort order or sort datatype kapy_kal XSLT 2 September 18th, 2007 02:10 PM
how to sort cross tab.sort based on row total joxa83 Crystal Reports 7 March 2nd, 2006 09:12 AM
Module.InsertText affecting unrelated variables! DaDeViL Access VBA 1 October 16th, 2005 12:43 AM
Unable to sort using xsl sort command sly_jimmy_boy XSLT 3 June 17th, 2005 05:15 AM
sort array of strings error lanita BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 1 December 14th, 2004 08:52 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.