XSLT - Appending strings to variable and printing it in a different file

arsalan_kashi

New Member
I have XML like this:\[code\]<article> <title>Article 1 title</title> <text>Lorem ipsum...</text> <image>http://example.com/img_01.jpg</image></article><article> <title>Article 2 title</title> <text>Dolor sit amet...</text> <image>http://example.com/img_02.jpg</image></article>\[/code\]I need to show just relative path to the image and I want the absolute path to be written in another file. Let's call it \[code\]img_to_download.html\[/code\].Here's my idea how to do it. I have one global variable called \[code\]image_src_download\[/code\] where I append one absolute path string during every iteration of for-each (using the function \[code\]f:download\[/code\] and giving it a parameter \[code\]image_src\[/code\]). Then when for-each is finished, I put content of variable \[code\]image_src_download\[/code\] to a separate file \[code\]img_to_download.html\[/code\].My XSLT looks like this:\[code\] <xsl:output method="html" encoding="UTF-8"/> <xsl:variable name="image_src"/> <!-- this is for a single entry--> <xsl:variable name="image_src_download"/> <!-- this should carry all sources to images --><!-- This function should append new string to the existing one in variable image_src_download --> <xsl:function name="f:download"> <xsl:param name="newLink"/> <xsl:variable name="image_src_download"> <xsl:value-of select="concat($image_src_download, $newLink, '\n')"/> </xsl:variable> </xsl:function> <xsl:template match="/"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <title>All articles</title> </head> <body> <xsl:for-each select="article"> <h1><xsl:value-of select="title"/></h1> <p><xsl:value-of select="text"/></p> <xsl:variable name="image_src"> <xsl:value-of select="image"/> </xsl:variable> <xsl:variable name="image_src_rel"> <xsl:value-of select="substring($image_src, 20)"/> <!-- Strips beggining of the absolute URL and leaves just relative path to the file --> </xsl:variable> <xsl:value-of select="f:download($image_src)"/> <!-- This should append absolute path string of the current article image to variable image_src_download --> <p><img src="http://stackoverflow.com/questions/10683343/{$image_src_rel}" /></p> </xsl:for-each> </body> </html> <!-- Generates new file where there are absolute paths to images on each line--> <xsl:result-document href="http://stackoverflow.com/questions/10683343/img_to_download.html" method="html"> <html> <head> <title>IMG to download</title> </head> <body> <xsl:value-of select="$image_src_download"/> </body> </html> </xsl:result-document> </xsl:template>\[/code\]My desired output are two files.File \[code\]articles.html\[/code\] that looks like this:\[code\]<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <title>All articles</title> </head> <body> <h1>Article 1 title</h1> <p>Lorem ipsum...</p> <p><img src="http://stackoverflow.com/questions/10683343/img_01.jpg"/></p> <h1>Article 2 title</h1> <p>Dolor sit amet...</p> <p><img src="http://stackoverflow.com/questions/10683343/img_02.jpg"/></p> </body></html>\[/code\]File \[code\]img_to_download.html\[/code\] that looks like this:\[code\]<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <title>IMG to download</title> </head> <body> <p> http://example.com/img_01.jpg<br/> http://example.com/img_02.jpg<br/> </p> </body></html>\[/code\]Please, do you have an idea how to make this work?All best, Johnny
 
Back
Top