Steve Waring

I do hope this is not considered off subject.

I've just created my first ever XSLT - Transformation (Style sheet).

http://www.stephenwaring.me.uk/products.xsl

Code Snippet

< xml version="1.0" encoding="ISO-8859-1" >
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Test</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Version</th><th>Features</th><th>Bug Fixes</th><th>Know Bugs</th>
</tr>
<xsl:for-each select="gadget/version">
<tr>
<td><xsl:value-of select="attribute::release" /><br /><xsl:value-of select="attribute::state" /></td>
<td><xsl:value-of select="feature" /></td>
<td><xsl:value-of select="bugFix" /></td>
<td><xsl:value-of select="knownBugs" /></td>
</tr>
</xsl:for-each>
</table>
<table border="1">
<tr bgcolor="#9acd32">
<th>Credits</th>
</tr>
<xsl:for-each select="gadget">
<tr>
<td>
<xsl:value-of select="credit" />
<xsl:if test="credit/attribute::ref != ''">
<br />Ref: <a><xsl:attribute name="href"><xsl:value-of select="credit/attribute::ref" /></xsl:attribute><xsl:value-of select="credit/attribute::ref" /></a>
</xsl:if>
<xsl:if test="credit/attribute::site != ''">
<br />Site: <a><xsl:attribute name="href"><xsl:value-of select="credit/attribute::site" /></xsl:attribute><xsl:value-of select="credit/attribute::site" /></a>
</xsl:if>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

With this file:

http://www.stephenwaring.me.uk/info1.xml

Code Snippet


< xml version="1.0" encoding="utf-8" >
< xml-stylesheet type="text/xsl" href="http://www.stephenwaring.me.uk/products.xsl" >
<gadget name="BoincStats" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.stephenwaring.me.uk/products.xsd">
<version release="2.1.0.0" state="pre-release">
<knownBugs>Will only work in English Locales, as different code is required for Font Family names in other languages.</knownBugs>
</version>
<version release="2.0.0.0" state="beta">
<knownBugs>Will only work in English Locales, as different code is required for Font Family names in other languages.</knownBugs>
</version>
<credit ref="http://forums.microsoft.com/MSDN/User/Profile.aspx UserID=457651%26SiteID=1">I am indebted to Jonathan Abbott for showing me how to obtain the list of fonts.</credit>
<credit>Working out what to do with the registery values to kludge them into real fonts was my own work.</credit>
</gadget>

The versions elements are displayed correctly, but only the first credit element is displayed. I can't for the life of me work out what is going wrong, but I'm guessing it is something really simple, as this is my first attempt at a transform.

It's probably easier to look at the source direct.




Re: XML and the .NET Framework Help please.

timvw

Just like with "gadget/version" you'll have to use <for-each select=""> for the versions...





Re: XML and the .NET Framework Help please.

Steve Waring

Could you explain a little bit more please

There is already a <xsl:for-each select="gadget"> around the credit code.






Re: XML and the .NET Framework Help please.

timvw

Yes, but that for-each loops over the gadgets.. Now in each of these loops, you need to loop over the versions..

Perhaps it's easier to understand if you think of it in code like c#:

foreach( gadget in gadgets )
{
foreach ( version in gadget.versions)
{
}
}






Re: XML and the .NET Framework Help please.

timvw

Apparently i was mistaken with my suggestion to loop over the versions... it should have been the credits offcourse...

(to make it up.. here is a working translation Stick out tongue.. I simply changed the last select="gadget" to select="gadget/credit" and change the references to get the wanted nodes in that for-each)

Code Snippet

< xml version="1.0" encoding="iso-8859-1" >
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Test</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Version</th>
<th>Features</th>
<th>Bug Fixes</th>
<th>Know Bugs</th>
</tr>
<xsl:for-each select="gadget/version">
<tr>
<td>
<xsl:value-of select="attribute::release" />
<br />
<xsl:value-of select="attribute::state" />
</td>
<td>
<xsl:value-of select="feature" />
</td>
<td>
<xsl:value-of select="bugFix" />
</td>
<td>
<xsl:value-of select="knownBugs" />
</td>
</tr>
</xsl:for-each>
</table>
<table border="1">
<tr bgcolor="#9acd32">
<th>Credits</th>
</tr>
<xsl:for-each select="gadget/credit">
<tr>
<td>
<xsl:value-of select="." />
<xsl:if test="./attribute::ref != ''">
<br />Ref: <a>
<xsl:attribute name="href">
<xsl:value-of select="./attribute::ref" />
</xsl:attribute>
<xsl:value-of select="./attribute::ref" />
</a>
</xsl:if>
<xsl:if test="./attribute::site != ''">
<br />Site: <a>
<xsl:attribute name="href">
<xsl:value-of select="./attribute::site" />
</xsl:attribute>
<xsl:value-of select="./attribute::site" />
</a>
</xsl:if>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>







Re: XML and the .NET Framework Help please.

Steve Waring

Many thanks, my first bit of xsl is now working. It will be made more sophisticated, and the translation applied via JavaScript.

Just one question. I can see why this code works, but I can't see why my code failed, when it does manage to step through the child nodes of <version>. Is it because <gadget> is the root level node

Thanks once again.






Re: XML and the .NET Framework Help please.

timvw

Your code iterated over the gadgets.. and then selected one (only one) version.