This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: simple XPath question
- To: <xsl-list at lists dot mulberrytech dot com>
- Subject: Re: [xsl] simple XPath question
- From: "Thomas B. Passin" <tpassin at mitretek dot org>
- Date: Mon, 2 Jul 2001 17:51:18 -0400
- References: <006901c10338$371d6610$86230240@siliconage.com>
- Reply-To: xsl-list at lists dot mulberrytech dot com
I got the same result as you with both Saxon and msxml3. Here's a way I
like, but it gives a slightly different output from what you suggest:
<xsl:template match='country'>
<xsl:choose>
<xsl:when test='position()=1'>
<first-country>
<xsl:value-of select='.'/>
</first-country>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select='.'/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
The result:
<storage>
<first-country>US</first-country>
<country>Canada</country>
</storage>
I've hit this behavior of xxx[1] once before, if I remember. Mike K or
someone will have to lay out for us precisely what xxx[1] is supposed to
mean in this context, I never ran it down.
Cheers,
Tom P
[Chris Nolte]
> I am trying to work through an example in Khun Yee Fung's XSLT book. It
is
> not giving me the solution he claims it should, but I do not understand
why
> not.
>
> Here is the sample XML:
> <?xml version='1.0'?>
> <warehouse>
> <item>
> <name>orange</name>
> <country>US</country>
> </item>
> <item>
> <name>ice wine</name>
> <country>Canada</country>
> </item>
> </warehouse>
>
> And here is the stylesheet:
> <?xml version='1.0'?>
> <xsl:stylesheet version='1.0'
> xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
> <xsl:output method='xml' indent='yes'/>
>
> <xsl:template match='/'>
> <xsl:apply-templates/>
> </xsl:template>
>
> <xsl:template match='warehouse'>
> <storage>
> <xsl:apply-templates select='item/country'/>
> </storage>
> </xsl:template>
>
> <xsl:template match='country'>
> <xsl:copy-of select='.'/>
> </xsl:template>
>
> <xsl:template match='country[1]'>
> <first-country>
> <xsl:copy-of select='.'/>
> </first-country>
> </xsl:template>
>
> </xsl:stylesheet>
>
> The output I am getting [using Saxon] is:
> <?xml version="1.0" encoding="utf-8"?>
> <storage>
> <first-country>
> <country>US</country>
> </first-country>
> <first-country>
> <country>Canada</country>
> </first-country>
> </storage>
>
> i.e., the first-country template is getting matched both times, even
though
> the XPath expression uses country[1]. What seems strange to me is that
when
> I include the line <xsl:copy-of select='position()'/> at the beginning of
> the template matching country[1], I get a 1 and a 2.
>
> I believe I am supposed to get:
> <storage>
> <first-country>
> <country>US</country>
> </first-country>
> <country>Canada</country>
> </storage>
>
> Am I doing something wrong, or is the example in Fung's book (Chapter 6,
p.
> 149) wrong? How should the stylesheet be structured to get the intended
> output?
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list