help with html table (nested for-each/keys/matched template)

Here should go questions about transforming XML with XSLT and FOP.
voithofer
Posts: 1
Joined: Sun Dec 18, 2005 12:09 pm

help with html table (nested for-each/keys/matched template)

Post by voithofer » Sun Dec 18, 2005 12:14 pm

Greetings,

I am trying to generate an html table that looks through the following xml source and lists links to all of the files (resource-file) and finds the resource-forms that match those file names and adds the appropriate file description next to the link to the filename. The xslt code that I've included doesn't work but gives an idea of what I'm trying to do. Through my research I believe that using keys and/or template matching my be the better way to go, but I'm not sure how to code them. I recognize that the xml structure is a little awkward but that's what I have to work with. Thanks in advance for any suggestions.


XML source

Code: Select all


<artifact>
<resource-file>
<artifact>
<displayName>file1.doc</displayName>
<uri>location of file1</uri>
</artifact>
<artifact>
<displayName>file2.doc</displayName>
<uri>location of file2</uri>
</artifact>
</resource-file>
<resource-form>
<file_name>file1.doc</file_name>
<resource_description>file1.doc description</resource_description>
</resource-form>
<resource-form>
<artifact>
<file_name>file2.doc</file_name>
<resource_description>file1.doc description</resource_description>
</artifact>
<artifact>
<file_name>file3.doc</file_name>
<resource_description>file3.doc description</resource_description>
</artifact>
<artifact>
<file_name>file4.doc</file_name>
<resource_description>file4.doc description</resource_description>
</artifact>
</resource-form>
</artifact>
Desired output

Resource File(s) File Description
(link to file1.doc) file1.doc description
(link to file2.doc) file2.doc description

nonworking xslt code

Code: Select all


<table>
<tr>
<td>Resource File(s)</td>
<td>File Description</td>
</tr>
<tr>
<xsl:for-each select="resource-file/artifact">
<xsl:variable name="filename" select="metaData/displayName"/>
<xsl:variable name="filelocation" select="fileArtifact/uri"/>
<xsl:for-each select="resource-form/artifact"['file_name=$filename']>
<tr>
<td>
<a>
<xsl:attribute name="href">
<xsl:value-of select="$filelocation"/>
</xsl:attribute>
<xsl:attribute name="target">blank</xsl:attribute>
<xsl:value-of select="$filename"/>
</a>
</td>
<td>
<b>Resource Description:</b>
<xsl:value-of select="structuredData/resource-form/resource_description"/>
</td>
</tr>
</xsl:for-each>
</xsl:for-each>
</tr>
</table>

george
Site Admin
Posts: 2101
Joined: Thu Jan 09, 2003 2:58 pm

Post by george » Mon Dec 19, 2005 1:12 pm

Hi,

Your stylesheet selects elements that are not even present in your sample XML file. You can find below a working sample (input, stylesheet, result) that may help you.

Code: Select all


<artifact>
<resource-file>
<artifact>
<displayName>file1.doc</displayName>
<uri>location of file1</uri>
</artifact>
<artifact>
<displayName>file2.doc</displayName>
<uri>location of file2</uri>
</artifact>
</resource-file>
<resource-form>
<artifact>
<file_name>file1.doc</file_name>
<resource_description>file1.doc description</resource_description>
` </artifact>
<artifact>
<file_name>file2.doc</file_name>
<resource_description>file2.doc description</resource_description>
</artifact>
<artifact>
<file_name>file3.doc</file_name>
<resource_description>file3.doc description</resource_description>
</artifact>
<artifact>
<file_name>file4.doc</file_name>
<resource_description>file4.doc description</resource_description>
</artifact>
</resource-form>
</artifact>

Code: Select all


<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/artifact">
<table>
<tr>
<td>Resource File(s)</td>
<td>File Description</td>
</tr>
<tr>
<xsl:for-each select="resource-file/artifact">
<xsl:variable name="filename" select="displayName"/>
<xsl:variable name="filelocation" select="uri"/>
<xsl:variable name="description"
select="/artifact/resource-form/artifact[file_name=$filename]/resource_description"/>
<tr>
<td>
<a>
<xsl:attribute name="href">
<xsl:value-of select="$filelocation"/>
</xsl:attribute>
<xsl:attribute name="target">blank</xsl:attribute>
<xsl:value-of select="$filename"/>
</a>
</td>
<td>
<b>Resource Description:</b>
<xsl:value-of select="$description"/>
</td>
</tr>
</xsl:for-each>
</tr>
</table>
</xsl:template>
</xsl:transform>

Code: Select all


Resource File(s)    File Description

file1.doc Resource Description:file1.doc description
file2.doc Resource Description:file2.doc description
Best Regards,
George

Post Reply