[英]Merging two XML source documents with XSLT
XML文件可以有1000-6000个表格; XML文件2可以具有1到100或更多。 我想用文件2替换文件1中的任何相同形式。 如果它存在于文件2中但不存在于文件1中,我想将其添加到文件1中。合并文件后,我想针对我的XSLT运行它。 我正在使用2.0样式表和Saxon解析器。
文件1:
<Forms>
<Form name="fred" date="10/01/2008"/>
<Form name="barney" date="12/31/2009"/>
<Form name="wilma" date="12/31/2010"/>
</Forms>
档案2:
<Forms>
<Form name="barney" date="01/31/2010"/>
<Form name="betty" date="6/31/2009"/>
</Forms>
合并的文件应如下所示:
<Forms>
<Form name="fred" date="10/01/2008"/>
<Form name="barney" date="01/31/2010"/>
<Form name="wilma" date="12/31/2010"/>
<Form name="betty" date="6/31/2009"/>
</Forms>
如果维护文档顺序不是优先事项:
<xsl:variable name="forms1" select="document('forms1.xml')/Forms/Form" />
<xsl:variable name="forms2" select="document('forms2.xml')/Forms/Form" />
<xsl:variable name="merged" select="
$forms1[not(@name = $forms2/@name)] | $forms2
" />
<xsl:template match="/">
<xsl:apply-templates select="$merged" />
</xsl:template>
<xsl:template match="Form">
<!-- for the sake of the example; you can use a more specialized template -->
<xsl:copy-of select="." />
</xsl:template>
如果出于任何原因优先考虑维护文档顺序...
<xsl:template match="/">
<!-- check values of file 1 sequentially, and replace them if needed -->
<xsl:for-each select="$forms1">
<xsl:variable name="this" select="." />
<xsl:variable name="newer" select="$forms2[@name = $this/@name]" />
<xsl:choose>
<xsl:when test="$newer">
<xsl:apply-templates select="$newer" />
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="$this" />
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
<!-- append any values from file 2 that are not in file 1 -->
<xsl:apply-templates select="$forms2[not(@name = $forms1/@name)]" />
</xsl:template>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.