XSLT合并数据
我有一个包含两组数据的xml文件,我需要将它们合并到一个显示新旧数据的表中。我通过只处理新数据来实现这一点,对于每一条新数据,我都会抓取相应的旧数据。我的问题是如何在新集合中正确存储我的当前位置,以便从旧集合中获取匹配数据。我面临的真正挑战是嵌套元素,其中position()没有那么有用。因此,考虑到以下简化的xml,我100%确定在新的和旧的xml中会有一个完全匹配的标记集XSLT合并数据,xslt,Xslt,我有一个包含两组数据的xml文件,我需要将它们合并到一个显示新旧数据的表中。我通过只处理新数据来实现这一点,对于每一条新数据,我都会抓取相应的旧数据。我的问题是如何在新集合中正确存储我的当前位置,以便从旧集合中获取匹配数据。我面临的真正挑战是嵌套元素,其中position()没有那么有用。因此,考虑到以下简化的xml,我100%确定在新的和旧的xml中会有一个完全匹配的标记集 <doc> <New> <Para>New info 1</
<doc>
<New>
<Para>New info 1</Para>
<Para>New info 2</Para>
<List>
<Li>New Point 1</Li>
<Li>New Point 2</Li>
<Li>New Point 3</Li>
</List>
<Para>New info 3</Para>
<List>
<Li>New Point 4</Li>
<Li>New Point 5</Li>
<Li>New Point 6
<List>
<Li>New nested Point 1</Li>
<Li>New nested Point 2</Li>
<Li>New nested Point 3</Li>
</List>
</Li>
</List>
</New>
<Old>
<Para>Old info 1</Para>
<Para>Old info 2</Para>
<List>
<Li>Old Point 1</Li>
<Li>Old Point 2</Li>
<Li>Old Point 3</Li>
</List>
<Para>Old info 3</Para>
<List>
<Li>Old Point 4</Li>
<Li>Old Point 5</Li>
<Li>Old Point 6
<List>
<Li>Old nested Point 1</Li>
<Li>Old nested Point 2</Li>
<Li>Old nested Point 3</Li>
</List>
</Li>
</List>
</Old>
新信息1
新信息2
新点1
新点2
新的第3点
新信息3
新的第4点
新的第5点
新的第6点
新嵌套点1
新嵌套点2
新嵌套点3
旧信息1
旧信息2
旧点1
旧点2
旧点3
旧信息3
旧点4
旧点5
旧点6
旧嵌套点1
旧嵌套点2
旧嵌套点3
我想合并并在类似html的表格中输出信息
<table>
<row>
<entry>New Info 1</entry>
<entry>Old Info 1</entry>
</row>
<row>
<entry>New Info 2</entry>
<entry>Old Info 2</entry>
</row>
<row>
<entry>
<list>
<li>New Point 1</li>
</list>
</entry>
<entry>
<list>
<li>Old Point 1</li>
</list>
</entry>
</row>
<row>
<entry>
<list>
<li>New Point 2</li>
</list>
</entry>
<entry>
<list>
<li>Old Point 2</li>
</list>
</entry>
</row>
<row>
<entry>
<list>
<li>New Point 3</li>
</list>
</entry>
<entry>
<list>
<li>Old Point 3</li>
</list>
</entry>
</row>
<row>
<entry>New Info 3</entry>
<entry>Old Info 3</entry>
</row>
<row>
<entry>
<list>
<li>New Point 4</li>
</list>
</entry>
<entry>
<list>
<li>Old Point 4</li>
</list>
</entry>
</row>
<row>
<entry>
<list>
<li>New Point 5</li>
</list>
</entry>
<entry>
<list>
<li>Old Point 5</li>
</list>
</entry>
</row>
<row>
<entry>
<list>
<li>New Point 6</li>
</list>
</entry>
<entry>
<list>
<li>Old Point 6</li>
</list>
</entry>
</row>
<row>
<entry>
<list>
<li>New nested Point 1</li>
</list>
</entry>
<entry>
<list>
<li>Old nested Point 1</li >
</entry>
</row>
<row>
<entry>
<list>
<li>New nested Point 2</li>
</list>
</entry>
<entry>
<list>
<li>Old nested Point 2</li>
</list>
</entry>
</row>
<row>
<entry>
<list>
<li>New nested Point 3</li>
</list>
</entry>
<entry>
<list>
<li>Old nested Point 3</li>
</list>
</entry>
</row>
</table>
新信息1
旧信息1
新信息2
旧信息2
新观点1
老一套
新的第2点
旧观点2
新的第3点
旧观点3
新信息3
旧信息3
新的第4点
旧观点4
新的第5点
旧观点5
新的第6点
老六点
新嵌套点1
旧嵌套点1
新嵌套点2
旧嵌套点2
新嵌套点3
旧嵌套点3
只有嵌套元素才有问题。对于其他所有内容,我都可以将位置存储在中并对相应的数据应用模板,但嵌套后,position()就没有那么有用了
关于如何有效地存储我的位置以便处理匹配元素,有什么想法吗?我使用的是XSLT 1。因为您的示例只处理每个分支中的顶级元素,所以请求的结果可以通过以下方式实现: XSLT1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="old" match="id" use="." />
<xsl:variable name="old" select="/doc/Old" />
<xsl:template match="/doc">
<table>
<xsl:apply-templates select="New/Para | New/List"/>
</table>
</xsl:template>
<xsl:template match="Para">
<xsl:variable name="i" select="count(preceding-sibling::Para)" />
<row>
<entry>
<xsl:value-of select="."/>
</entry>
<entry>
<xsl:value-of select="$old/Para[$i + 1]"/>
</entry>
</row>
</xsl:template>
<xsl:template match="List">
<xsl:variable name="i" select="count(preceding-sibling::List)" />
<row>
<entry>
<xsl:copy-of select="."/>
</entry>
<entry>
<xsl:copy-of select="$old/List[$i + 1]"/>
</entry>
</row>
</xsl:template>
</xsl:stylesheet>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- first-pass -->
<xsl:variable name="old">
<xsl:apply-templates select="/doc/Old" mode="old"/>
</xsl:variable>
<xsl:variable name="old-set" select="exsl:node-set($old)" />
<xsl:template match="Para | Li" mode="old">
<node>
<xsl:attribute name="path">
<xsl:for-each select="ancestor-or-self::*">
<xsl:value-of select="name()" />
<xsl:variable name="i" select="count(preceding-sibling::*[name()=name(current())])" />
<xsl:text>[</xsl:text>
<xsl:value-of select="count(preceding-sibling::*[name()=name(current())])" />
<xsl:text>]</xsl:text>
<xsl:if test="position()!=last()">
<xsl:text>/</xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:attribute>
<xsl:value-of select="text()"/>
</node>
<xsl:apply-templates select="*" mode="old"/>
</xsl:template>
<!-- output -->
<xsl:template match="/doc">
<table>
<xsl:apply-templates select="New"/>
</table>
</xsl:template>
<xsl:template match="Para | Li">
<xsl:variable name="path">
<xsl:for-each select="ancestor-or-self::*">
<xsl:value-of select="name()" />
<xsl:variable name="i" select="count(preceding-sibling::*[name()=name(current())])" />
<xsl:text>[</xsl:text>
<xsl:value-of select="count(preceding-sibling::*[name()=name(current())])" />
<xsl:text>]</xsl:text>
<xsl:if test="position()!=last()">
<xsl:text>/</xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="old-path" select="concat('doc[0]/Old[0]/', substring-after($path, 'doc[0]/New[0]/'))" />
<row>
<entry>
<xsl:value-of select="text()"/>
</entry>
<entry>
<xsl:value-of select="$old-set/node[@path=$old-path]"/>
</entry>
</row>
<xsl:apply-templates select="*"/>
</xsl:template>
</xsl:stylesheet>
结果
<?xml version="1.0" encoding="UTF-8"?>
<table>
<row>
<entry>New info 1</entry>
<entry>Old info 1</entry>
</row>
<row>
<entry>New info 2</entry>
<entry>Old info 2</entry>
</row>
<row>
<entry>New Point 1</entry>
<entry>Old Point 1</entry>
</row>
<row>
<entry>New Point 2</entry>
<entry>Old Point 2</entry>
</row>
<row>
<entry>New Point 3</entry>
<entry>Old Point 3</entry>
</row>
<row>
<entry>New info 3</entry>
<entry>Old info 3</entry>
</row>
<row>
<entry>New Point 4</entry>
<entry>Old Point 4</entry>
</row>
<row>
<entry>New Point 5</entry>
<entry>Old Point 5</entry>
</row>
<row>
<entry>New Point 6
</entry>
<entry>Old Point 6
</entry>
</row>
<row>
<entry>New nested Point 1</entry>
<entry>Old nested Point 1</entry>
</row>
<row>
<entry>New nested Point 2</entry>
<entry>Old nested Point 2</entry>
</row>
<row>
<entry>New nested Point 3</entry>
<entry>Old nested Point 3</entry>
</row>
</table>
新信息1
旧信息1
新信息2
旧信息2
新观点1
老一套
新的第2点
旧观点2
新的第3点
旧观点3
新信息3
旧信息3
新的第4点
旧观点4
新的第5点
旧观点5
新的第6点
老六点
新嵌套点1
旧嵌套点1
新嵌套点2
旧嵌套点2
新嵌套点3
旧嵌套点3
我不理解您的输出。值“新列表1”
和“新列表2+嵌套列表”
从何而来?我试图简明扼要地说明我需要列表内容和嵌套列表内容。我将更新为clearerI,但不幸的是,我确实需要匹配单个节点。我已经相应地更新了输出。基本上,每次在中有一个元素时,我都需要立即处理@user3198443中的对应元素,您使用的是哪个特定的XSLT处理器?您可能会从使用扩展函数中受益。很抱歉延迟,但不幸的是,我使用的是msxml。@user3198443这个问题有答案吗?如果没有,为什么没有?我没有看到更新的答案。谢谢你的帮助。