[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] AW: document() and position()
Jeni, thanks for your answer but it hasn't solved my problem. My description of the problem was not well. Lets try it with this one: Let's say I have four xml files. Each of them have a root node like <news show='true'> ... </news> The 'show' attribute can change from file to file. Its either 'true' or 'false'. Every of the xml files is displayed in the xml stream I get. It shows like this: <page> <folder>1.xml</folder> <folder>2.xml</folder> <folder>3.xml</folder> <folder>4.xml</folder> </page> Let's say the files '2.xml' and '4.xml' have a @show = 'true'. The other two are 'false'. As you mentioned, I need the index of the file within the files I actually retrieving (i.e. only those for whom @show = 'true'). If I do anything like this: <xsl:template match="/"> <xsl:apply-templates select="document(/page/folder)/news[@show = 'true']" /> </xsl:template> <xsl:template match="news"> <xsl:variable name="index" select="position()" /> News item <xsl:value-of select="$index" /> ... </xsl:template> I will allways get '1'. Because the 'news' tag is allways the number one position in the current xml file. When I apply the templates with this: <xsl:apply-templates select="document(/page/folder)/news[@show = 'true']"> it should choose the file '2.xml' and give me the number '1' and it should choose the file '4.xml' and give me the number '2'. The access to the file works, but I never found out to become the right number. hope this description of my problem is better Dirk -------------------------------------------- doubleSlash Net-Business GmbH Muellerstr. 12/1 88045 Friedrichshafen Fon 07541 60 47-102 Fax 07541 60 47-111 www.doubleslash.de dirk.holstein@xxxxxxxxxxxxxx > > >But now I need to know the number of the actual file I > >read in the template I call. I tried something like this: > > > ><xsl:apply-templates select="document(/page/folder)/news[@show = > 'true']"> > > <xsl:with-param name="index"><xsl:value-of select="position()" > >/></xsl:with-param> > ></xsl:apply-templates> > > > >But the index parameter is allways '1'. How can I get > >the position? Or is there any other way to get the > >number of the actual xml file? > > Within your xsl:apply-templates above, you set the value of the parameter > $index to the value of the position() of the current node within the > current node list. The xsl:apply-templates doesn't do anything to change > the value of the current node, so if this is in a template like: > > <xsl:template match="/"> > <xsl:apply-templates select="document(/page/folder)/news[@show > = 'true']"> > <xsl:with-param name="index"> > <xsl:value-of select="position()" /> > </xsl:with-param> > </xsl:apply-templates> > </xsl:template> > > then the current node when setting the parameter value is still the root > node of the input document. As there is only one of these, the value is > always 1. > > I'm not exactly sure which position you are interested in: the > index of the > file within the files that you're actually retrieving (i.e. only those for > whom @show = 'true') or the index of the file within all the > files that you > could have retrieved. > > If you want to find the position of the 'news' element amongst the other > 'news' elements that you're actually retrieving, you want to use the > position() function when the current node is the 'news' element you're > interested in and the current node list is the list of the 'news' elements > that you've retrieved. This is the situation within the 'news'-matching > templates that are applied using the xsl:apply-templates you've specified > above, so try something like: > > <xsl:template match="/"> > <xsl:apply-templates select="document(/page/folder)/news[@show > = 'true']" /> > </xsl:template> > > <xsl:template match="news"> > <xsl:variable name="index" select="position()" /> > News item <xsl:value-of select="$index" /> > ... > </xsl:template> > > If you want to find the position of the 'news' element amongst the 'news' > elements that you *could* have retrieved, you want to use the position() > function when the current node is the 'news' element you're interested in > and the current node list is the list of all the 'news' elements, whether > @show='true' or not. In that case, try something like: > > <xsl:template match="/"> > <xsl:apply-templates select="document(/page/folder)/news" /> > </xsl:template> > > <xsl:template match="news"> > <xsl:variable name="index" select="position()" /> > <xsl:if test="@show = 'true'"> > News item <xsl:value-of select="$index" /> > ... > </xsl:if> > </xsl:template> > > I hope that this helps, > > Jeni > > Dr Jeni Tennison > Epistemics Ltd * Strelley Hall * Nottingham * NG8 6PE > tel: 0115 906 1301 * fax: 0115 906 1304 * email: > jeni.tennison@xxxxxxxxxxxxxxxx > > XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|