替换或不替换 - 递归调用模板或应用模板在 XSL 1.0 结果树碎片烦恼



我有一个源文档,需要使用从另一个来源提取/导入/计算的RTF进行合并和丰富。我需要合并片段中的入站内容值以替换等效节点存在的现有内容值,并在它们不存在时添加。

我已经尝试了无数次解决这个问题,但我不是专家,并且确信我使问题过于复杂。请参阅下面的微弱尝试和失败,并提供源文档和导入片段的示例以及预期结果。请帮帮我!

在过去的几周里,我尝试组合了许多类似的问题解决方案,但这些示例都使用值而不是导入的片段。我不太明白我做错了什么!

源文档 .XML:

<?xml version="1.0" encoding="UTF-8"?>
<sourceDocument>
<lotsOfChildNodes />
<thisBitToUpdate>
<repeatedElements>
<repeatedElement>
<repeatedElementName>element1</repeatedElementName>
<repeatedElementContent>Content1</repeatedElementContent>
</repeatedElement>
<repeatedElement>
<repeatedElementName>element2</repeatedElementName>
<repeatedElementContent>Content2</repeatedElementContent>
</repeatedElement>
<repeatedElement>
<repeatedElementName>element3</repeatedElementName>
<repeatedElementContent>Content3</repeatedElementContent>
</repeatedElement>
<repeatedElement>
<repeatedElementName>element4</repeatedElementName>
<repeatedElementContent>Content4</repeatedElementContent>
</repeatedElement>
</repeatedElements>
</thisBitToUpdate>
<lotsMoreChildNodes />
</sourceDocument>

导入的 RTF XML:

<repeatedElements>
<repeatedElement>
<repeatedElementName>element2</repeatedElementName>
<repeatedElementContent>Content2Updated</repeatedElementContent>
</repeatedElement>
<repeatedElement>
<repeatedElementName>element4</repeatedElementName>
<repeatedElementContent>Content4Updated</repeatedElementContent>
</repeatedElement>
<repeatedElement>
<repeatedElementName>element5</repeatedElementName>
<repeatedElementContent>Content5New</repeatedElementContent>
</repeatedElement>
<repeatedElement>
<repeatedElementName>element6</repeatedElementName>
<repeatedElementContent>Content6New</repeatedElementContent>
</repeatedElement>
</repeatedElements>

XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="xsl">
<xsl:import href="routineGeneratingContentToMerge"/>
<xsl:template match="/ | @* | node()">
<copy>
<xsl:apply-templates select="@* | node()" />
</copy>
</xsl:template>
<xsl:template match="/sourceDocument/lotsOfChildNodes/thisBitToUpdate/repeatedElements">
<xsl:variable name="nodeName" select="localName()" />
<xsl:variable name="newElementCount" select="1" />
<xsl:variable name="newElements" select="exsl:node-set($importedRepeatedElements)/repeatedElements" />
<element name="$nodeName">
<call-template name="replaceDupesAndAdd">
<xsl:with-param name="positionCount" select="$newElementCount" />
<xsl:with-param name="elementsToMerge" select="$newElements" />
<xsl:with-param name="currentElements" select="." />
</call-template>
</element>

</xsl:template>
<xsl:template name="replaceDupesAndAdd">
<xsl:param name="positionCount" />
<xsl:param name="elementsToMerge" />
<xsl:param name="currentElements" />
<xsl:variable name="countElementsToMerge" select="count($elementsToMerge/repeatedElement)" />
<xsl:variable name="mergeElementName" >
<xsl:value-of select="$elementsToMerge/repeatedElement[position()=$positionCount]/repeatedElementName" />
</xsl:variable>
<xsl:if test="$positionCount &lt;= $countElementsToMerge">
<xsl:for-each select="exile:node-set($currentElements)/repeatedElement" >
<xsl:variable name="elementName">
<xsl:value-of select="./repeatedElementName">
</xsl:variable>
<xsl:choose>
<xsl:when test="$elementName=$mergeElementName">
<xsl:copy-of select="$elementsToMerge/repeatedElement[repeatedElementName=$mergeElementName]" />
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="." />
<xsl:otherwise>
</xsl:choose>
<xsl:for-each>
<xsl:variable name="newCount">
<xsl:value-of select="number($positionCount+'1')" />
</xsl:variable>
<!-- call-template name="replaceDupesAndAdd">
<xsl:with-param name="positionCount" select="$newCount" />
<xsl:with-param name="currentElements" select="." />
<xsl:call-template -->
</xsl:if>

</xsl:template>
</xsl:stylesheet>

期望的结果 源文档 .XML:

<?xml version="1.0"?>
<sourceDocument>
<lotsOfChildNodes />
<thisBitToUpdate>
<repeatedElements>
<repeatedElement>
<repeatedElementName>element1</repeatedElementName>
<repeatedElementContent>Content1</repeatedElementContent>
</repeatedElement>
<repeatedElement>
<repeatedElementName>element2</repeatedElementName>
<repeatedElementContent>Content2Updated</repeatedElementContent>
</repeatedElement>
<repeatedElement>
<repeatedElementName>element3</repeatedElementName>
<repeatedElementContent>Content3</repeatedElementContent>
</repeatedElement>
<repeatedElement>
<repeatedElementName>element4</repeatedElementName>
<repeatedElementContent>Content4Updated</repeatedElementContent>
</repeatedElement>
<repeatedElement>
<repeatedElementName>element5</repeatedElementName>
<repeatedElementContent>Content5New</repeatedElementContent>
</repeatedElement>
<repeatedElement>
<repeatedElementName>element6</repeatedElementName>
<repeatedElementContent>Content6New</repeatedElementContent>
</repeatedElement>
</repeatedElements>
</thisBitToUpdate>
<lotsMoreChildNodes />
</sourceDocument>

考虑跨不同条件拉入片段文档节点:1( 仅在源文档中,2( 也在源文档中,以及 3( 仅在片段文档中。

XSLT 1.0

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" encoding="UTF-8"/>
<xsl:strip-space elements="*"/>

<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="repeatedElements">
<xsl:copy>
<xsl:variable name="fragment" select="document('RepeatElementsMergeDoc_Fragment.xml')/descendant::repeatedElement"/>
<xsl:variable name="curr_val" select="repeatedElement/repeatedElementName"/>

<xsl:copy-of select="repeatedElement[not(repeatedElementName = $fragment/repeatedElementName)]"/>   
<xsl:copy-of select="$fragment[repeatedElementName = $curr_val]" /> 
<xsl:copy-of select="$fragment[not(repeatedElementName = $curr_val)]" />    
</xsl:copy>
</xsl:template>  

</xsl:stylesheet>

输出

<?xml version="1.0" encoding="UTF-8"?>
<sourceDocument>
<lotsOfChildNodes/>
<thisBitToUpdate>
<repeatedElements>
<repeatedElement>
<repeatedElementName>element1</repeatedElementName>
<repeatedElementContent>Content1</repeatedElementContent>
</repeatedElement>
<repeatedElement>
<repeatedElementName>element3</repeatedElementName>
<repeatedElementContent>Content3</repeatedElementContent>
</repeatedElement>
<repeatedElement>
<repeatedElementName>element2</repeatedElementName>
<repeatedElementContent>Content2Updated</repeatedElementContent>
</repeatedElement>
<repeatedElement>
<repeatedElementName>element4</repeatedElementName>
<repeatedElementContent>Content4Updated</repeatedElementContent>
</repeatedElement>
<repeatedElement>
<repeatedElementName>element5</repeatedElementName>
<repeatedElementContent>Content5New</repeatedElementContent>
</repeatedElement>
<repeatedElement>
<repeatedElementName>element6</repeatedElementName>
<repeatedElementContent>Content6New</repeatedElementContent>
</repeatedElement>
</repeatedElements>
</thisBitToUpdate>
<lotsMoreChildNodes/>
</sourceDocument>

相关内容

  • 没有找到相关文章

最新更新