Welcome to WebmasterWorld Guest from 54.162.93.137

Forum Moderators: httpwebwitch

Message Too Old, No Replies

Question about XSL stylesheet

     
2:06 pm on Apr 1, 2010 (gmt 0)

5+ Year Member



I'm trying to develop a generic stylesheet that will work with any XML document, regardless of the names of its nodes, but I'm not sure how to reference them .

For example, using the example from the W3C schools website,

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select="catalog">
<xsl:for-each select="cd">
<tr>
<td><xsl:value-of select="title" /></td>
<td><xsl:value-of select="artist" /></td>
</tr>
</xsl:for-each>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

This style sheet specifies specific node names such as 'catalog', 'cd','title', 'artist, etc. What would you do if you just wanted it to output the nodes generically without the need to specify a name, so that the style sheet would work with all xml documents of the same format?

I tried using 'root' and 'firstChild', etc but it didn't seem to work, any ideas?
6:22 am on Apr 3, 2010 (gmt 0)

WebmasterWorld Administrator httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member



you'll need to sharpen your Axes [w3.org]
7:46 am on Apr 7, 2010 (gmt 0)

5+ Year Member



I am interested to know what this Axes is about. I have been using a lot of HTML and XSL,but had not really thought about this.
11:53 am on Apr 7, 2010 (gmt 0)

WebmasterWorld Administrator httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Axes are plural of "axis" - a strange usage of the word, but it's the name of a device that lets an XPATH expression refer to a parent, ancestor, sibling, descendent-or-self, etc.

An axis is a selector that can find a node based on its relationship to the current node, regardless of its name or type. You can get the first child of a <book>, without knowing that it's a <chapter>.

Knowing XPATH is useful in all finds of XML parsing situations, not just in XSLT templates. If you are familiar with CSS selectors, XPATH has a lot in common with those.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month