This is an adapted follow-up question to my previous one.
I have got to xml files referring to one another by the corresp
-attribute.
main.xml
<body>
<div type="section">
<div type="subsection">
<l id="A01" corresp="B01"><note>First</note> line of A</l>
<l id="A02" corresp="B02">Second line of A</l>
</div>
</div>
</body>
to_merge.xml
<body>
<div type="section">
<div type="subsection">
<l id="B01" corresp="A01">First line of B</l>
<l id="B02" corresp="A02">Second <note>line</note> of B</l>
</div>
</div>
</body>
My XSL stylesheet trans.xsl (created following the kind suggestions by @michael-hor257k and @martin-honnen):
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="utf-8" indent="yes"/>
<xsl:key name="ref" match="body/div/div/l" use="@id"/>
<xsl:template match="/body">
<html>
<head>
<title>Sample</title>
</head>
<body>
<xsl:apply-templates select="//div/div">
<xsl:apply-templates/>
</xsl:apply-templates>
</body>
</html>
</xsl:template>
<xsl:template match="div/div">
<table>
<xsl:apply-templates select="l"/>
</table>
</xsl:template>
<xsl:template match="l">
<xsl:variable name="corresp" select="@corresp"/>
<tr>
<td><xsl:apply-templates/></td>
<td>
<xsl:for-each select="document('to_merge.xml')">
<xsl:value-of select="key('ref', $corresp)"/>
</xsl:for-each>
</td>
</tr>
</xsl:template>
<xsl:template match="note">
<b><xsl:value-of select="."/></b>
</xsl:template>
</xsl:stylesheet>
The stylesheet almost does what I'd like it to do. The produced output:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Sample</title>
</head>
<body><table>
<tr>
<td>
<b>First</b> line of A</td>
<td>First line of B</td>
</tr>
<tr>
<td>Second line of A</td>
<td>Second line of B</td>
</tr>
</table></body>
</html>
The problem is that I cannot define a template to process the <note>
-element in the imported document in exactly the same way as the main.xml, that is I would like to have <td>Second <b>line</b> of B</td>
in the last cell of the last row.
Do I have to choose a completely different approach?
Many thanks!
The <xsl:apply-templates/>
is a short-hand for <xsl:apply-templates select="node()"/>
, meaning it processes the child nodes of the context node, which in your template is an l
element.
Inside of the xsl:for-each select="document('to_merge.xml')"
the context node is the secondary input and the key
function call key('ref', $corresp)
selects an l
element in that document. So you want to apply-templates
to the child nodes of that l
element, meaning instead of the xsl:value-of
you have you want to use <xsl:apply-templates select="key('ref', $corresp)/node()"/>
.