xsl-list
[Top] [All Lists]

Re: listing links in xsl?

2003-12-17 12:49:23

"james walker" <jameswalkerandy(_at_)hotmail(_dot_)com> wrote in message
news:Law9-F24bcsDFCa0fjh00027067(_at_)hotmail(_dot_)com(_dot_)(_dot_)(_dot_)
given this xml structure (there are many links inside root):
<root>
<link>
<title>.......</title>
<url>....</url>
<description>.....s</description>
</link>
<root>

i am trying to list the links on a web page by ndl links (links that
contain
'.ndl') and then by html links. So far i have created a key:
xsl:key name="ndl-links" match="link" use="contains(url, '.ndl')=true" />

This is a bit confusing -- it is equivalent to the simpler:

 <xsl:key name="ndl-links" match="link"
    use="contains(url, '.ndl')" />


which i though would assign a key to the links which contain '.ndl'?

when i try and loop through the key it doesnt show the ndl links, however
it
does show the html links?

<xsl:template match="root">
<ul>

<xsl:for-each select="key('ndl-links', contains(url, '.ndl')=true)">

This will not return anything, because the current node "root" does not have
"url" children.


Here's a simple transformation that produces the output you want:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
 <xsl:output omit-xml-declaration="yes"/>

 <xsl:key name="ndl-links" match="link"
    use="contains(url, '.ndl')" />

  <xsl:template match="/">
     Links with URL containing ".ndl":
     <xsl:copy-of select="key('ndl-links', true())"/>
     &#xA;Links with URL not containing ".ndl":
     <xsl:copy-of select="key('ndl-links', false())"/>
  </xsl:template>
</xsl:stylesheet>

When applied on your source.xml (corrected to make it well-formed):

<root>
  <link>
    <title>.......</title>
    <url>.ndl</url>
    <description>.....s</description>
  </link>
  <link>
    <title>.......</title>
    <url>.html</url>
    <description>.....s</description>
  </link>
  <link>
    <title>.......</title>
    <url>.ndl2</url>
    <description>.....s</description>
  </link>
</root>

the wanted result is produced:


     Links with URL containing ".ndl":
     <link>
    <title>.......</title>
    <url>.ndl</url>
    <description>.....s</description>
  </link><link>
    <title>.......</title>
    <url>.ndl2</url>
    <description>.....s</description>
  </link>

Links with URL not containing ".ndl":
     <link>
    <title>.......</title>
    <url>.html</url>
    <description>.....s</description>
  </link>


Hope this helped.


Dimitre Novatchev.
FXSL developer.

http://fxsl.sourceforge.net/ -- the home of FXSL
Resume: http://fxsl.sf.net/DNovatchev/Resume/Res.html




 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



<Prev in Thread] Current Thread [Next in Thread>