I have two XML documents with different structures (examples below) that I want to merge in the XSLT document I use for creating HTML output.
What I want to do is to create a table like this (example with cars):
Serialnr Brand Color Year Price Condition
123456 'Opel' 'Blue' '1978' '10000$' 'Used'
... ... ... ... ... ...
The last two of these (Price and Condition) are to be found in the second document (foo2.xml), and the matching between these documents is the serialnr. The cars isn’t ordered in the same order in the documents, and it exist more cars in foo2.xml (that won’t be used) than in foo1.xml.
foo1.xml:
<cars>
<car>
<serialnr>123456</serialnr>
<brand>Opel</brand>
<color>Blue</color>
<year>1978</year>
</car>
...and so on...
foo2.xml:
<vehicles>
<vehicle>
<cell nr="2"><data nr="3">123456</data></vehicle> // <--- match on serialnr (123456)
<cell><data nr="3">10000$</data></cell>
<cell><data nr="3">Used</data></cell>
...and so on...
Foo1.xml is connected to the XSLT document with PHP on the server side, and foo2.xml is merged in the XSLT document (see below).
cars.xsl:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
<xsl:param name="foo2" select="'foo2.xml'"/>
<xsl:template match="/">
<xsl:text disable-output-escaping='yes'><!DOCTYPE html></xsl:text>
<html>
<head>
<title>Producers</title>
</head>
<body>
...//table, thead, tr, th...
<tbody>
<xsl:for-each select="//car">
<tr>
<td>
<xsl:value-of select="serialnr" />
</td>
<td>
<xsl:value-of select="brand" />
</td>
<td>
<xsl:value-of select="color" />
</td>
<td>
<xsl:value-of select="year" />
</td>
</xsl:for-each>
<xsl:for-each select="document($foo2)//Vehicle">
// I need to match the serialnr in some way to get the correct car's data here, but how?
<td>
// Selecting Cell node 2 (price)
//<xsl:value-of select="/? "/>
</td>
<td>
// Selecting Cell node 3 (condition)
//<xsl:value-of select="/? "/>
</td>
So, how can I do this? I really appreciate help here. Thanks a lot in advance!
Notes:
- The structure of foo2.xml isn’t consistent. Suddenly the child node changes from being “vehicle” to “part” (i.e. “vehicles/vehicle” to “vehicles/part”).
- In foo2.xml there are a lot of “vehicle” nodes that contains of data I’m not interested in, so I suppost I must use some sort of if statement to test whether the desired data exists or not when looping through them?
Taking a guess at the answers to the questions that I posted in the main comment feed, please find this XSLT 1.0 solution.
With this document as the main input…
… and this document as a readable document resource passed in to the style-sheet with parameter name
foo2……this XSLT 1.0 style-sheet…*
…will yield this output (a HTML5 document) …
Note
A solution using keys would also be possible.