我最近问了一个关于如何忽略多个元素的问题,并在使用“前置”和 Muenchian 方法方面得到了一些很好的回应。但是我想知道是否可以使用索引 xml 文件跨多个文件执行此操作。
索引.xml
<?xml-stylesheet type="text/xsl" href="merge2.xsl"?>
<list>
<entry name="File1.xml" />
<entry name="File2.xml" />
</list>
XML 文件示例
<Main>
<Records>
<Record>
<Description>A</Description>
</Record>
<Record>
<Description>A</Description>
</Record>
<Record>
<Description>B</Description>
</Record>
<Record>
<Description>C</Description>
</Record>
</Records>
<Records>
<Record>
<Description>B</Description>
</Record>
<Record>
<Description>A</Description>
</Record>
<Record>
<Description>C</Description>
</Record>
<Record>
<Description>C</Description>
</Record>
</Records>
</Main>
合并2.xsl
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="xml" indent="yes" />
<xsl:key name="Record-by-Description" match="Record" use="Description"/>
<xsl:template match="@* | node()">
<xsl:apply-templates select="@* | node()"/>
</xsl:template>
<xsl:template match="Main">
<table>
<tr>
<th>Type</th>
<th>Count</th>
</tr>
<xsl:apply-templates select="Records"/>
</table>
</xsl:template>
<xsl:template match="Records">
<xsl:apply-templates select="Record[generate-id() = generate-id(key('Record-by-Description', Description)[1])]" mode="group"/>
</xsl:template>
<xsl:template match="Record" mode="group">
<tr>
<td>
<xsl:value-of select="Description"/>
</td>
<td>
<xsl:value-of select="count(key('Record-by-Description', Description))"/>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
这在一个文件上运行良好,并为我提供了生成一张表的所需结果,仅显示唯一项目并添加计数。但是,在浏览多个文件的 index.xml 时,我无法产生所需的结果。
我尝试使用针对 index.xml 的单独模板并将“主”模板应用于不同的 XML 文件,还尝试使用 for-each 循环浏览不同的文件。
在被介绍给 Muenchian 方法之前,我使用带有“preceding”的 for-each 来检查重复节点,但是“preceding”似乎只在当前文档中搜索,并且无法找到有关在多个文档中使用它的信息。
使用这些方法中的任何一种都可以在多个文档中搜索重复的元素文本吗?
非常感谢您的帮助。