Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question about loops - new to xml

Status
Not open for further replies.

tsowerby

Programmer
Jul 24, 2007
2
GB
Hi, only just started working with xml and I was wondering if anyone can help me out-
I have an 'expose' photo album displaying 4 photos using the following code -

<?xml version="1.0" encoding="utf-8"?>
<expose version="2.1">
<picture _mngid="1">
<title>18point6</title>
<image>
<url>tim/18point6/18point6_01.jpg</url>
</image>
<smallimage>
<url>tim/18point6/_sm_18point6_01.jpg</url>
</smallimage>
<largeimage>
<url>tim/18point6/_lg_18point6_01.jpg</url>
</largeimage>
</picture>
<picture _mngid="2">
<title>18point6</title>
<image>
<url>tim/18point6/18point6_02.jpg</url>
</image>
<smallimage>
<url>tim/18point6/_sm_18point6_02.jpg</url>
</smallimage>
<largeimage>
<url>tim/18point6/_lg_18point6_02.jpg</url>
</largeimage>
</picture>
<picture _mngid="3">
<title>18point6</title>
<image>
<url>tim/18point6/18point6_03.jpg</url>
</image>
<smallimage>
<url>tim/18point6/_sm_18point6_03.jpg</url>
</smallimage>
<largeimage>
<url>tim/18point6/_lg_18point6_03.jpg</url>
</largeimage>
</picture>
<picture _mngid="4">
<title>18point6</title>
<image>
<url>tim/18point6/18point6_04.jpg</url>
</image>
<smallimage>
<url>tim/18point6/_sm_18point6_04.jpg</url>
</smallimage>
<largeimage>
<url>tim/18point6/_lg_18point6_04.jpg</url>
</largeimage>
</picture>
</expose>




However adding more photos may take ages so creating a loop would be much easier as the only change between each photo is an incrementing number.
Seems it should be easy to do but I can't find an appropriate example.

Thanks for any help anyone can give me.

Tom
 
[1] The serial (_03.jpg, _04.jpg etc), if any, of the picture .jpg should be maintained at the level of the creation of the picture itself. It should not be maintained via the xml document source.

[2] In the xml as a store of data, you should not make the attribute _mngid reflecting the "document order" of the picture nodes inside the document, if that is what you meant for _mngid. One can easily imagine picture got created and deleted over time. That "document order" won't be maintained easily at the raw data storage level.

[2.1] If you need one attribute reflecting that order, you should do it as a house-keeping task, using for instance a fixed xslt to output a derived document updating that order. A consequence of it is that you don't need to worry about the assigning _mngid at the data entry level.

[3]>However adding more photos may take ages...
Why is that? Data takes time to be entered for use by operators, that's for sure. With the consideration of [2], you make the operation a no-brainer and the operators have less to worry about and improve efficiency. Leave the indexing to the house-keeping repetitive xslt routine.

In any case, the question would be too vague and too broad and resource dependent.
 
not sure if I was clear. The document already does what it's meant to.
I'd just like to put it in a loop rather than have each <picture> block.
e.g. -
variable number of images = i
for (n=0, n<=i, n++) {
<picture>
<title>18point6</title>
<image>
<url>tim/18point6/18point6_i.jpg</url>
</image>
etc...
</picture>
}

(hope that makes some sense)

Sure it's quite simple, i'm literally just starting with this and have no clue...
Thanks


 
not sure if I was clear.
No, your issue is still not well described.

However, assuming that you need to be able to do this loop, I will describe and illustrate how one 'loops' in XSLT. As you probably have discovered, XSLT does not any looping or assignment (i.e. i = i + 1). However, it does have recursion, and that is how one solves problems that require looping in a procedural language.

Here is an illustration:
Code:
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
<xsl:output indent="yes"/>

<xsl:template match="/">
<root>
<xsl:call-template name="digitSequence">
	<xsl:with-param name="theValue" select="7"/>
</xsl:call-template>
</root>	
</xsl:template>

<xsl:template name="digitSequence">
<xsl:param name="theValue"/>
<xsl:if test="$theValue &gt; 0">
<xsl:call-template name="digitSequence">
	<xsl:with-param name="theValue" select="$theValue - 1"/>
</xsl:call-template>
<output><xsl:value-of select="concat('abc_',format-number($theValue,'00'),'.jpg')"/></output>
</xsl:if>
</xsl:template>
</xsl:stylesheet>

This is the output:
Code:
<root>
  <output>abc_01.jpg</output>
  <output>abc_02.jpg</output>
  <output>abc_03.jpg</output>
  <output>abc_04.jpg</output>
  <output>abc_05.jpg</output>
  <output>abc_06.jpg</output>
  <output>abc_07.jpg</output>
</root>

Tom Morrison
 
What language are you working with for the for-loop? As a programmer, you don't feel that piece of info as crucial?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top