473,241 Members | 1,508 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 software developers and data experts.

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

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.

Rick

XML source

<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

<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>
Dec 18 '05 #1
2 1667
rick wrote:
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.
You dont need XSL for such a trivial data extraction.
The following script in xmlgawk will do all the data
extraction described:

BEGIN { XMLMODE=1 }
XMLCHARDATA { data = $0 }
XMLENDELEM == "displayName" { name = data }
XMLENDELEM == "uri" { uri[name] = data }
XMLENDELEM == "resource_description" { split(data, d); dsc[d[1]] = data }

END {
for (n in uri) {
print uri[n] "\t" n "\t" dsc[n]
}
}

When running the script, you will get the result:

gawk -lxml -f artifact.awk artifact.xml
location of file1 file1.doc file1.doc
description
location of file2 file2.doc file2.doc
description

Notice the line break in the output, which are caused by
your XML data. By the way, there is a bug in your XML data.
<artifact>
<file_name>file2.doc</file_name>
<resource_description>file1.doc here: ^^^^^^^^ description</resource_description>
</artifact>

Dec 18 '05 #2
rick wrote:
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,
Definitely. for-each is almost always the wrong answer :-)
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.
It would be useful -- both to you and your data-supplier -- to meet
with a consultant to discuss improving the efficiency of the XML
through redesigned document markup. The current design is seriously
crippled by the fact that the resource-form element type can apparently
contain either a single file_name/resource_description pair OR multiple
occurrences of the artifact element type, and that the artifact element
type has two apparently mutually exclusive content models, one for
display_name and uri, and one for file_name and resource_description.
Handling inconsistencies like this costs extra time and money, which
you and/or your supplier can save by improving the data model.
Desired output

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


<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:output method="html" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:key name="description" match="resource_description"
use="preceding-sibling::file_name"/>

<xsl:template match="/">
<table>
<tr>
<th>Resource File</th>
<th>File Description</th>
</tr>
<xsl:apply-templates
select="artifact/resource-file/artifact/displayName"/>
</table>
</xsl:template>

<xsl:template match="displayName">
<tr>
<td>
<a href="{following-sibling::uri}">
<xsl:apply-templates/>
</a>
</td>
<td>
<xsl:value-of select="key('description',.)"/>
</td>
</tr>
</xsl:template>

</xsl:stylesheet>

This produces:

<table>
<tr>
<th>Resource File</th>
<th>File Description</th>
</tr>
<tr>
<td><a href="location of file1">file1.doc</a></td>
<td>file1.doc description</td>
</tr>
<tr>
<td><a href="location of file2">file2.doc</a></td>
<td>file1.doc description</td>
</tr>
</table>

///Peter
--
XML FAQ: http://xml.silmaril.ie/

Dec 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: | last post by:
Hello, here is the code: <? $a=array(1,2,3,4,5,6,7,8,9,10); $b=array(1,3,5,7,9); $c=array(1,1,2,2,3,3); $d=array_count_values($b); $e=array_count_values($c);
3
by: Sandros | last post by:
Background: I'm collecting usability statistics for a group of applications. Each count has the following attributes: date, application, major heading, minor heading, count. My intent is to pull...
4
by: Gareth Gale | last post by:
I'm trying to implement a way of allowing a user to sort a HTML table via Javascript on the client. I've seen lots of samples where single column sorting (asc or desc) is shown, but I'd like nested...
2
by: Michael Meckelein | last post by:
Environment: VS.NET 2003 Application: ASP.NET web application Program language: C# (Csharp) Situation: I get some information from a sql database. With this information I generate a html table...
5
by: Arjen | last post by:
Hi, The login control (<asp:Login) generates a HTML table. I have used my own template without table's. The control generates <table> ... my design(template) ... </table>. I don't like the...
4
by: cardinallijr | last post by:
Hi all, sorry about the simple question but I'm new to XSL. I have a XML with 5 elements, for example: <root> <products> <product> <id>1</id> </product> <product>
7
by: Lorenzino | last post by:
Hi, I have a problem with bindings in a formview. I have a formview; in the insert template i've created a wizard control and inside it i have an HTML table with some textboxes bound to the...
4
by: McGowan | last post by:
Hi, I'm trying to display data from a mysql database in a HTML table but for some reason my code isn't working. At the moment I have got it to read and display the headers and the first row of the...
2
by: gomako | last post by:
Hi, I'm new, so please let me know if any of my forum etiquette is wrong! Apologies for the fairly nondescript subject line, but I am being driven insane by it. Anyhow, I have a form with...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.