Link to home
Start Free TrialLog in
Avatar of tia_kamakshi
tia_kamakshiFlag for United Arab Emirates

asked on

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,
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

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)
Avatar of tia_kamakshi

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Many Many Thanks