Updating variable value in loop in xslt

Hi,

For first article , I was creating url variable

<xsl:variable name="url" select="concat($base, $pathIs)"/>

which depends on other 2 variables as below:

<xsl:variable name="firstNews" select="article[1]" />
<xsl:variable name="pathIs" select="$firstNews/@xml"></xsl:variable>

Now, I have article variable in loop. I understand the variable value once assigned cannot be changed in XSLT
Therefore, I need to setup value for variable firstNews as per article pasition.
Something like this:
      <xsl:variable name="firstNews" select="article[position()]" />

Please can you advise that below code is correct. If not please help me correcting.
I cannot test it before tomorrow morning.

<xsl:for-each select="article[position() &gt; 1 and position() &lt; 3]">

	<xsl:variable name="firstNews" select="article[position()]" />
	<xsl:variable name="pathIs" select="$firstNews/@xml"></xsl:variable>
	<xsl:variable name="url" select="concat($base, $pathIs)"/>
	
	<img width="246" height="175" class="latest-news-item-thumb">
		<xsl:attribute name="src">
			<xsl:value-of select="concat($imgPath, '/', document($url)//image[@type='S']/src)"/>
		</xsl:attribute>
	</img>
	
</xsl:for-each>

Open in new window

I am working on ASP.net2.0 using C#


Please advise.

Regards,
tia_kamakshiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Geert BormansInformation ArchitectCommented:
Hmm, I don't see what you are trying to do

<xsl:for-each select="article[position() &gt; 1 and position() &lt; 3]">
      <xsl:variable name="firstNews" select="article[position()]" />

inside the for each the context is the article
so the variable should rather be like this
      <xsl:variable name="firstNews" select="." />

but I don't see you using the variable inside the for-each.
Notice that a variable does not live outside its scope (in this case the variable $firstNews can only be used inside the for-each)
0
tia_kamakshiAuthor Commented:
Hi Gertone,

Thanks for your reply

I have variable called $base where it has the value where image resides for all the articles

My below xslt is reading summary xml something like this

<?xml version="1.0" encoding="utf-16"?>
<newsindex>
	<article id="944714"  xml="Website\xslt\ReadingAnotherXmlWithinXSLT\944714.xml" date="20120802134700" image="Phuket-tn_tcm133-944615.jpg" publish="20120802134700">
		<title>News 1</title>
		<summary>Lorem1 ipsum dolor sit amet.</summary>
	</article>
	<article id="944130"  xml="Website\xslt\ReadingAnotherXmlWithinXSLT\9447145.xml" date="20120802134700" image="Phuket-tn_tcm133-944634.jpg" publish="20130802134700">
		<title>News 2</title>
		<summary>Lorem2 ipsum dolor sit amet.</summary>
	</article>
	
</newsindex>

Open in new window

In the xml attribute above, I have the path of detail xml where all the details can be seen.
From this detail xml I wanted to fetch the image name having [@type='S']/src

So, from the below code basically I wanted to fetch the image path:
            <xsl:attribute name="src">
                  <xsl:value-of select="concat($imgPath, '/', document($url)//image[@type='S']/src)"/>
            </xsl:attribute>


<xsl:for-each select="article[position() &gt; 1 and position() &lt; 3]">

	<xsl:variable name="firstNews" select="article[position()]" />
	<xsl:variable name="pathIs" select="$firstNews/@xml"></xsl:variable>
	<xsl:variable name="url" select="concat($base, $pathIs)"/>
	
	<img width="246" height="175" class="latest-news-item-thumb">
		<xsl:attribute name="src">
			<xsl:value-of select="concat($imgPath, '/', document($url)//image[@type='S']/src)"/>
		</xsl:attribute>
	</img>
	
</xsl:for-each> 

Open in new window


Please advise
0
Geert BormansInformation ArchitectCommented:
That should work actualy, though you don't need the variables

<xsl:for-each select="article[position() &gt; 1 and position() &lt; 3]">
	<img width="246" height="175" class="latest-news-item-thumb">
		<xsl:attribute name="src">
			<xsl:value-of select="concat($imgPath, '/', document(concat($base, @xml))//image[@type='S']/src)"/>
		</xsl:attribute>
	</img>
	
</xsl:for-each>  

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tia_kamakshiAuthor Commented:
Many Many Thanks
0
Geert BormansInformation ArchitectCommented:
welcome
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.