This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: simple XPath question
James Melton wrote:
> The match='country[1]' will match any country which is the first country
> node of its parent. Both "country" elements are the first "country"
> elements of their parent "item" nodes, so both are output inside a
> "first-country" element. If you were to add another "country" node
> inside of either "item" node you would see that it is not wrapped in a
> "first-country" element.
Thanks. In playing around with it I had sort of discovered this already,
but I didn't really understand it. Your explanation helps a lot.
Now that I think about it some more, I believe in the previous chapter the
author wrote that elements in node-sets do not have an order. Thus it could
not be that select='item/country' could select the two <country> nodes,
country[1] being US and country[2] being Canada.
And the position() values are really a separate thing entirely. They are
positions in the source tree, not in the node-set, since nodes in node-sets
don't have positions.
(Do I have this right?)
Instead the source XML would have to be something like
<warehouse>
<item>
<country>US</country>
<country>Canada</country>
</item>
</warehouse>
for first-country to match US and country to match Canada.
<grumble>It does make one wonder whether any editor actually tried working
the examples in the book.</grumble>
> Chris Nolte wrote:
> >
> > 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?
> >
> > Chris
> >
> > XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
>
> --
>
> ____________________________________________________________
> James Melton CyLogix
> 609.750.5190 609.750.5100
> james.melton@cylogix.com www.cylogix.com
>
> XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
>
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list