【发布时间】:2020-06-20 12:06:48
【问题描述】:
这是对my previous one 的修改后的后续问题。
我必须通过 corresp-attribute 来引用彼此的 xml 文件。
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>
我的 XSL 样式表 trans.xsl(根据 @michael-hor257k 和 @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>
样式表几乎可以做我想做的事。产生的输出:
<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>
问题是我无法定义一个模板来处理导入文档中的<note>-元素,其方式与main.xml完全相同,即我想要@ 987654328@ 在最后一行的最后一个单元格中。
我必须选择完全不同的方法吗?
非常感谢!
【问题讨论】: