How do I transform a XML doc using XSTL when I don’t know the names of the nodes. So basically it should work universally, with any XML document.
Let’s say I got this XML document:
<?xml version="1.0"?>
<?xml-stylesheet href="transform.xsl" type="text/xsl" ?>
<!DOCTYPE cinema [
<!ELEMENT a (b*)>
<!ELEMENT b (c,d,e)>
<!ELEMENT c (#PCDATA)>
<!ELEMENT d (#PCDATA)>
<!ELEMENT e (#PCDATA)>
]>
<cinema>
<movie>
<actor>Some actor</actor>
<title>Some title</title>
<year>Some year</year>
</movie>
</cinema>
How would I create an HTML table out of this? I know I can match the root element like this:
<xsl:template match="/">
Then I select all movies like this:
<xsl:for-each select="/*/*">
But how to I know get one row for each movie with three columns for actor, title & year? Especially since the XML file (movie) should be able to maybe only have 2 children or 5.
+++EDIT+++
If I do this:
<tr>
<td><xsl:value-of select="."/></td>
</tr>
I get each movie’s details in one row. This is almost close to what I want to achieve. But how to I spread out the movie details over three columns in that row?
If you know you will always have three levels of elements (root, children and grandchildren) then something like this should do it:
The
/template matches the document node,/*matches first-level element children of the document node (i.e. the root element of the document),/*/*matches second-level children, etc.<xsl:apply-templates/>(with noselect) will apply matching templates to all the child nodes of the current node, which includes child elements, text nodes, comments and processing instructions. As a result, you may need the root template to beto select only child elements.