xml - 将 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>
我必须选择完全不同的方法吗?
非常感谢!
解决方案
是的<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()"/>
的 .
推荐阅读
- android - 为 AndroidViewModel 创建 ViewModelFactory 的正确方法是什么
- java - JUnit 测试 - 如何检查扫描仪的各种输入不匹配
- sql - SQL Server:如何更改 sa 的密码?
- php - MVC 模式:如果模型需要调用其视图的函数,如何解决这个问题?
- amazon-s3 - spark.read.parquet 在 Colab 中不起作用
- javascript - 正则表达式:匹配所有字符串,但捕获两个符号之间的组
- javascript - req.user 是未定义的节点 + 快递 + 护照 + 猫鼬
- c++ - 对象单击场景时如何找出对象接口类指针?
- c# - 如何使用 LINQ 比较 char 数组的元素?
- google-apps-script - 从另一张工作表调用查询并将其更改为较低且正确的