Altova Mailing List Archives>Archive Index >microsoft.public.xml Archive Home >Recent entries >Thread Prev - Question node array dynamic ???? [Thread Next] Re: Question node array dynamic ????To: NULL Date: 5/14/2009 12:11:00 PM Ssemi™ wrote:
> here xml data
>
> <section title="country" view="1">
> <submenu title="Bangkok">
> <submenu title="Manila">
> <submenu title="Cebu">
> </section
Consider to post well-formed XML, the above is far from being
well-formed. Assuming the XML is as follows:
<section title="country" view="1">
<submenu title="Bangkok"/>
<submenu title="Manila"/>
<submenu title="Cebu"/>
</section>
> xsl data
>
> <xsl:template match="/">
> <xsl:apply-templates select="//section" />
> </xsl:template>
>
> <xsl:template match="section">
> <xsl:variable name="viewnum" select="@view" />
> <xsl:value-of select="submenu[$viewnum]/@title" />
> </xsl:template>
>
> result
> Nothing.
then I can't reproduce the problem with the following stylesheet:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="/">
<xsl:apply-templates select="//section" />
</xsl:template>
<xsl:template match="section">
<xsl:variable name="viewnum" select="@view" />
<xsl:value-of select="submenu[$viewnum]/@title" />
</xsl:template>
</xsl:stylesheet>
as it does not output nothing but rather 'Bangkog'.
However doing submenu[$viewnum]/@title is probably not what you want,
you rather want
submenu[position() = $viewnum]/@title
as that way you get what you want, namely you select the 'title'
attribute value for that 'submenu' element which is at position $viewnum
of all 'submenu' child elements.
Doing
submenu[$viewnum]
where $viewnum is a string as in your case would convert that string to
a boolean value and any non-empty string converts to true meaning
submenu[$viewnum]
is
submenu[true()]
which is the same as
submenu
and finally
<xsl:value-of select="submenu[$viewname]/@title
would always output the 'title' attribute of the first 'submenu' child.
--
Martin Honnen --- MVP XML
http://msmvps.com/blogs/martin_honnen/
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
