|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
loop function in XSL?
If I have the following tags in XML:
Code:
<DIR name="files">
<FILE name="hello.txt">
<FILE name="hello.bat">
<DIR name="HELP">
<FILE name="README.TXT"/>
<FILE name="help.hlp"/>
</DIR>
</DIR>
<DIR name="images">
<FILE name="logo.gif"/>
</DIR>
So 2 directories off root "files" and "images", the files dir has 2 files and another dir. When I do my XSL like this: Code:
<xsl:template match="DIR">
DIR: <xsl:value-of select="@name"/><BR/>
FILES:<BR/>
<xsl:for-each select="FILE">
<xsl:value-of select="@name"/><BR/>
</xsl:for-each>
</xsl:template>
I only get the FILE tags, But I want both like a explorer view, so I put "*" instead of "FILE" this does all <DIR> and <FILE> inside the top <DIR> great. But the <DIR> tag inside the top<DIR> tag gets renders here, and shows as a file, and I want the whole <DIR> code to start again for this object. Is that possible? Thanks.
__________________
regards, pgudge |
|
#2
|
||||
|
||||
|
I like to provide working code in my replies, but this is all theory (meaning I've never done something like this exactly), and I just don't have time to test and tweak to get a working example. But, here's what I would try doing ... create a template to handle the FILEs. You can then call this template using apply-templates. In this template, you will have to use an "if" to determine if the tag is a FILE or a DIR and then apply-templates again. So you'll apply-templates recursively like you might have a recursive function in a programming language. You might have to get real tricky here, using current(), node(), or any other function available. W3 Schools is a good place to get some quick examples and a list of functions.
__________________
# Jeremy Explain your problem instead of asking how to do what you decided was the solution. |
|
#3
|
|||
|
|||
|
You don't even need a loop:
Code:
<xsl:template match="DIR">
DIR: <xsl:value-of select="@name"/><BR/>
<xsl:if test="count(FILE) != 0">
FILES:
</xsl:if>
<xsl:apply-templates />
</xsl:template>
<xsl:template match="FILE">
<xsl:value-of select="@name"/><BR/>
</xsl:template>
I haven't tested the code (not enough time), but basically, what you are doing is: DIR: - write the name of the directory - test if the number of FILE tags is different than 0, and if so, write "FILES:" - apply-templates apply-templates means that XSLT will apply whatever template is necessary, based on what your XML contains. If it finds a DIR tag, it will use the DIR template, if it finds a FILE tag, it will use the FILE template. Hope this helps ![]() |
|
#4
|
||||
|
||||
|
have you looked into <xsl:for-each select='DIV'>
? you could get this to work say for every div you get print out a / and a break and then a tab ? -Teflon
__________________
Teflon - The Black <desc>Mark This Up</desc> |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > XML Programming > loop function in XSL? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|