0

Hello Everyone, I am fairly new to xml and xslt/xpath, but I am hoping someone can help me with the following problem:

I have a software system that accepts a query and returns an xml file. I want to be able to use xslt to create an html table from the elements in the xml file. I have no control over the format of the xml file that is returned by the software system. In a nutshell, the information returned is file names and file descriptions located in a folder. If there is a single file in the folder, the xml file will have a format of something like this:

<root>
<path>\\someserver\someshare\
<folder>Folder 1
<files>File1.pdf</files>
<descrs>File 1 Description</descrs>
</folder>
</path>
</root>

When there are multiple files in the folder, the xml file will have a format of something like this:

<root>
<path>\\someserver\someshare\
<folder>Folder 1
<files>
<v1>File1.pdf</v1>
<v2>File2.pdf</v2>
<v3>File3.pdf</v3>
<v4>File4.pdf</v4>
</files>
<descrs>
<v1>File 1 Description</v1>
<v2>File 2 Description</v2>
<v3>File 3 Description</v3>
<v4>File 4 Description</v4>
</descrs>
</folder>
</path>
</root>

What I need to do is associate (or match) each file with its corresponding file description...and that is where I am getting lost. My desired html table would have the format of:

COL1 COL2 COL3
Path value File 1 name File 1 description
Path value File 2 name File 2 description
Path value File 3 name File 3 description
Path value File 4 name File 4 description

Any help would be greatly appreciated.

Thanks,
Kenny

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by iceandrews
0

I did something quick and dirty to try and get what you wanted. This is what I came up with. It hinges on that there will be the same number of files and descriptions and that the order of the files and descriptions are the ones related to each other. Basically what's happening is the following. I'm matchin on any child of files, and for each one of those I'm creating a row. I populate the path and file name easily. Then for each file, I note the order of the <vX> element using the position. I then find the desrcs/vX that has the same position.

Using the input document below.

<root>
	<path>\\someserver\someshare\
		<folder>Folder 1
			<files>
				<v1>File1.pdf</v1>
				<v2>File2.pdf</v2>
				<v3>File3.pdf</v3>
				<v4>File4.pdf</v4>
			</files>
			<descrs>
				<v1>File 1 Description</v1>
				<v2>File 2 Description</v2>
				<v3>File 3 Description</v3>
				<v4>File 4 Description</v4>
			</descrs>
		</folder>
	</path>
</root>

This is the transformation that I wrote.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

	<xsl:variable name="getpath" select="//path/text()"/>

	<xsl:template match="/">
		<html>
			<body>
				<table border="1">
					<tr>
						<th>Path</th>
						<th>File Name</th>
						<th>Description</th>
					</tr>
					<xsl:apply-templates select="//files/*"/>
				</table>
			</body>
		</html>
	</xsl:template>

	<xsl:template match="files/*" >
		<tr>
			<td>
				<xsl:value-of select="$getpath"/>
			</td>
			<td>
				<xsl:value-of select="." />
			</td>
			<td>
				<xsl:variable name="getfileposition" select="position()" />
				<xsl:value-of select="ancestor::folder/descrs/*[$getfileposition]" />
			</td>
		</tr>
	</xsl:template>
</xsl:stylesheet>

The result is the following HTML code.

<html>
	<body>
		<table border="1">
			<tr>
				<th>Path</th>
				<th>File Name</th>
				<th>Description</th>
			</tr>
			<tr>
				<td>\\someserver\someshare\</td>
				<td>File1.pdf</td>
				<td>File 1 Description</td>
			</tr>
			<tr>
				<td>\\someserver\someshare\</td>
				<td>File2.pdf</td>
				<td>File 2 Description</td>
			</tr>
			<tr>
				<td>\\someserver\someshare\</td>
				<td>File3.pdf</td>
				<td>File 3 Description</td>
			</tr>
			<tr>
				<td>\\someserver\someshare\</td>
				<td>File4.pdf</td>
				<td>File 4 Description</td>
			</tr>
		</table>
	</body>
</html>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.