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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to use an element's position to create links 2

Status
Not open for further replies.

TonyMarston

Programmer
Dec 8, 1999
20
0
0
GB
I am trying to extract the value of an element's position in the XML file so that I can create references to the current, previous and next element. Can anyone give a newbie some advice?

My XML file looks something like this:

<?xml version=&quot;1.0&quot;?>
<?xml-stylesheet type=&quot;text/xsl&quot; href=&quot;filelist.xsl&quot;?>
<DIRECTORY>
<FILE>
<NAME>picture1.jpg</NAME>
</FILE>
<FILE>
<NAME>picture2.jpg</NAME>
</FILE>
.....
<FILE>
<NAME>picture99.jpg</NAME>
</FILE>
</DIRECTORY>

My XSL file looks like this:

<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl=&quot;<xsl:template match=&quot;/&quot;>
<html>
<body>
<table border=&quot;0&quot; width=&quot;100%&quot;>
<xsl:for-each select=&quot;DIRECTORY/FILE&quot;>
<tr>
<td valign=&quot;top&quot;><a href=&quot;#imageN-1&quot;>PREV</a></td>
<td valign=&quot;top&quot;><a href=&quot;#imageN+1&quot;>NEXT</a></td>
<td valign=&quot;top&quot; align=&quot;center&quot;>
<a name= &quot;imageN&quot;>
<xsl:element name=&quot;img&quot;>
<xsl:attribute name=&quot;src&quot;><xsl:value-of select=&quot;NAME&quot;/></xsl:attribute>
<xsl:attribute name=&quot;alt&quot;><xsl:value-of select=&quot;NAME&quot;/></xsl:attribute>
</xsl:element>
</a>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

'imageN' should indicate the current element number.
'imageN-1' should indicate the previous element number.
'imageN+1' should indicate the next element number.
 
Hello,
The problem you are asking could be solved with using new Naming Space In this naming space there are functions which you need like position(), last(), count(), name() etc.
But you cannot do directly XSLT transformation, you will need some kind of server-side (or client-side) processing (like ASP for server-side example).
If you could use such environment(MS XML Parser, ASP), I'll post new xsl file.
Hope this helps.
D.
 
A new XSL file for an MS XML parser would be helpful, thanks.
 
Hello,
It will be long post.
Files you need:
dir.asp
dir.xml
dir.xsl

dir.asp code:
======================================================
<%
'Load the XML
set oXmlDoc = Server.CreateObject(&quot;MSXML2.DOMdocument&quot;)
oXmlDoc.async = false
oXmLDoc.load(Server.MapPath(&quot;dir.xml&quot;))

'Load the XSL
set oXslDoc = Server.CreateObject(&quot;MSXML2.DOMdocument&quot;)
oXslDoc.async = false
oXslDoc.load(Server.MapPath(&quot;dir.xsl&quot;))

'Transform the file
Response.Write(oXmlDoc.transformNode(oXslDoc))
%>

=====================================================
dir.xml code: (removed a direct transformation)
=====================================================
<?xml version=&quot;1.0&quot;?>
<DIRECTORY>
<FILE>
<NAME>picture1.jpg</NAME>
</FILE>
<FILE>
<NAME>picture2.jpg</NAME>
</FILE>
.....
<FILE>
<NAME>picture99.jpg</NAME>
</FILE>
</DIRECTORY>
====================================================
dir.xsl code: (with new namingspace)
====================================================

<?xml version=&quot;1.0&quot;?>
<xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot; <xsl:template match=&quot;/&quot;>
<html>
<body>
<table border=&quot;0&quot; width=&quot;100%&quot;>
<xsl:apply-templates select=&quot;DIRECTORY/FILE&quot;/>
</table>
</body>
</html>
</xsl:template>

<xsl:template match=&quot;FILE&quot;>
<tr>
<td valign=&quot;top&quot;>
<xsl:choose>
<xsl:when test=&quot;position() = 1&quot;>PREV</xsl:when>
<xsl:eek:therwise>
<a>
<xsl:attribute name=&quot;href&quot;>#image<xsl:value-of select=&quot;position()-1&quot;/></xsl:attribute>PREV
</a>
</xsl:eek:therwise>
</xsl:choose>
</td>
<td valign=&quot;top&quot; align=&quot;center&quot;>
<a>
<xsl:attribute name=&quot;name&quot;>image<xsl:value-of select=&quot;position()&quot;/></xsl:attribute>
<xsl:element name=&quot;img&quot;>
<xsl:attribute name=&quot;src&quot;><xsl:value-of select=&quot;NAME&quot;/></xsl:attribute>
<xsl:attribute name=&quot;alt&quot;><xsl:value-of select=&quot;NAME&quot;/></xsl:attribute>
</xsl:element>
</a>
</td>
<td valign=&quot;top&quot;>
<xsl:choose>
<xsl:when test=&quot;position() = last()&quot;>NEXT</xsl:when>
<xsl:eek:therwise>
<a>
<xsl:attribute name=&quot;href&quot;>#image<xsl:value-of select=&quot;position()+1&quot;/></xsl:attribute>NEXT
</a>
</xsl:eek:therwise>
</xsl:choose>
</td>
</tr>
</xsl:template>

</xsl:stylesheet>

=======================================================
A little explanation of dir.xsl

I used &quot;template - apply-templates&quot; technique, because I found it more powerful than &quot;for-each&quot;. Actually it is the same thing, but you can divide the needed transformation into templates and apply what and when needed.

position() - returns the index number of the node within the parent. The first node returns a position of 1.
last() - returns a number equal to context size of the node
xsl:choose, xsl:when and xsl:eek:therwise are used make if-then-else conditional statement.
In this case I used it to assure that the PREV of first picture and NEXT of last picture will not have links.

==================================================
Hope this helps.
if there are further questions, post again :)
D.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top