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 December 10th, 2007, 10:23 AM
Authorized User
 
Join Date: Nov 2006
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default Converting 1 to January etc.

I want to convert
Code:
        <postedAt>
            <year>2007</year>
            <month>12</month>
            <day>10</day>
        </postedAt>
to

Code:
December 2007
I'm currently using
Code:
    <xsl:template match="postedAt">
        <xsl:apply-templates select="month" mode="word"/>
        <xsl:text> </xsl:text>
        <xsl:apply-templates select="year"/>
    </xsl:template>

    <x:date>January</x:date>
    <x:date>February</x:date>
    <x:date>March</x:date>
    <x:date>April</x:date>
    <x:date>May</x:date>
    <x:date>June</x:date>
    <x:date>July</x:date>
    <x:date>August</x:date>
    <x:date>September</x:date>
    <x:date>October</x:date>
    <x:date>November</x:date>
    <x:date>December</x:date>

    <xsl:template match="month" mode="word">
        <xsl:value-of select="document('')//x:date[position() = .]" />
    </xsl:template>
the last template in that code doesn't appear to be working. What I don't understand is that this works:
Code:
document('')//x:date[position() = 12]
with the 12 instead of .
and yet . contains 12 (I verified this with value-of). I tried using number() but that made no difference.

 
Old December 10th, 2007, 10:38 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

The problem is that "." in inside the square brackets [] is the current context. This changes inside a predicate.

It might be better to use call template

<xsl:template name="word-to-month">
  <xsl:param name="num" as="xs:integer"/>
  <xsl:value-of select="document('')//x:date[position() = $num]" />
</xsl:template>

And then:

<xsl:call-template name="word-to-month">
  <xsl:with-param name="num" select="xs:integer(month)"/>
</xsl:call-template>

If you're not using XSLT 2.0 then skip the as="xs:integer" bit obviously.

/- Sam Judson : Wrox Technical Editor -/
 
Old December 10th, 2007, 11:02 AM
Authorized User
 
Join Date: Nov 2006
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks samjudson. I forgot about the predicate context change behaviour.

 
Old December 10th, 2007, 11:07 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The context (.) inside a predicate is different from the context outside the predicate. Inside the predicate it refers to the node currently being tested. Use current() instead.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting dates outspaced XSLT 1 November 27th, 2006 01:31 PM
Converting V.B.6 to V.B.2005 - Help ! liamfitz Intro Programming 1 October 31st, 2006 12:36 PM
Converting NULL to 0 rit01 SQL Server 2000 2 March 7th, 2006 09:52 AM
Converting cookies value. rupen Javascript How-To 6 June 8th, 2005 02:34 AM
Any One Help me Type Converting yoord BOOK: Beginning ASP.NET 1.0 0 September 21st, 2004 04:17 AM





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