【发布时间】:2020-03-07 14:36:39
【问题描述】:
我知道这里有很多关于通过 xslt 从外部 xml 文件查找的问题(和答案)。但是,我还没有完全理解关键功能的逻辑,所以我很难将其他解决方案应用到我的用例中。
我有两个 xml 文件:
versA.xml
<TEI>
<div>
<l id="A001" corresp="B001">First line of VersA</l>
<l id="A002" corresp="B002">Second line of VersA</l>
<l id="A003" corresp="B003">Third line of VersA</l>
</div>
</TEI>
和
versB.xml
<TEI>
<div>
<l id="B001" corresp="A001">First line of VersB</l>
<l id="B002" corresp="A002">Second line of VersB</l>
<l id="B003" corresp="A003">Third line of VersB</l>
</div>
</TEI>
文件通过corresp-attribute 相互引用。
我正在尝试找出一个解析 versA.xml、打印自己的文本节点然后查找相应文本的 xsl 样式表 (trans.xsl) versB.xml
中的节点trans.xsl
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:variable name="vB" select="document('versB.xml')/TEI/div/l"/>
<xsl:template match="/TEI/div/l">
I found ID <xsl:value-of select="@id"/> in versA.xml.
How can I get the corresponding node in versB.xml which has the ID <xsl:value-of select="@corresp"/>?
</xsl:template>
</xsl:stylesheet>
我能做的是输出 versA.xml 的 ids 并访问 versB.xml。但是,我发现设置一个适当的键函数非常困难,该函数从 versA.xml 中获取 corresp 值以在 versB.xml
如果有人能解释如何实现这一点,我会很高兴。
出于兼容性原因,xslt 版本 1.0 将是首选。
我已经根据 cmets 中给出的建议更新了我的样式表。以下给出了所需的输出:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:key name="ref" match="TEI/div/l" use="@id"/>
<xsl:template match="/TEI/div/l">
<xsl:variable name="corresp" select="@corresp"/>
<xsl:value-of select="."/> corresponds to
<xsl:for-each select="document('versB.xml')">
<xsl:value-of select="key('ref', $corresp)"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
【问题讨论】: