首页 > 解决方案 > 将 XSL 模板应用于通过键功能选择的元素

问题描述

这是对我之前的问题的修改后的后续问题。

corresp我必须通过-attribute来引用彼此的xml 文件。

主要的.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>

问题是我无法定义一个模板来处理导入文档中的 -element ,其方式与main.xml<note>完全相同,即我想在最后一行的最后一个单元格中使用。<td>Second <b>line</b> of B</td>

我必须选择完全不同的方法吗?

非常感谢!

标签: xmlxsltxslt-1.0tei

解决方案


是的<xsl:apply-templates/>简写<xsl:apply-templates select="node()"/>,意味着它处理上下文节点的子节点,在您的模板中是一个l元素。

xsl:for-each select="document('to_merge.xml')"上下文节点内部是辅助输入,key函数调用key('ref', $corresp)选择l该文档中的一个元素。所以你想要apply-templates那个l元素的子节点,而不是xsl:value-of你想要使用<xsl:apply-templates select="key('ref', $corresp)/node()"/>的 .


推荐阅读