【问题标题】:apply XSL template to element that is selected via key function将 XSL 模板应用于通过键功能选择的元素
【发布时间】:2020-06-20 12:06:48
【问题描述】:

这是对my previous one 的修改后的后续问题。

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

ma​​in.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>

问题是我无法定义一个模板来处理导入文档中的&lt;note&gt;-元素,其方式与ma​​in.xml完全相同,即我想要@ 987654328@ 在最后一行的最后一个单元格中。

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

非常感谢!

【问题讨论】:

    标签: xml xslt xslt-1.0 tei


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-08
      • 1970-01-01
      • 2011-11-06
      • 2017-09-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多