删除变量XSLT 2.0中的重复项 我想保留第一次出现的重复主题。重复主题可能出现在层次结构中的任何级别 在我最初的XSLT中,输入XML将是一个变量
Input.xml作为变量($Ixml)删除变量XSLT 2.0中的重复项 我想保留第一次出现的重复主题。重复主题可能出现在层次结构中的任何级别 在我最初的XSLT中,输入XML将是一个变量,xslt,xslt-2.0,Xslt,Xslt 2.0,Input.xml作为变量($Ixml) 预期-Output.xml($Oxml) 根据建议,我已将XSLT更新如下:--------------------------------------------------------- <xsl:key name="removeDuplicate" match="topic" use="@href"/> <xsl:template match="/"> <xsl:
预期-Output.xml($Oxml)
根据建议,我已将XSLT更新如下:---------------------------------------------------------
<xsl:key name="removeDuplicate" match="topic" use="@href"/>
<xsl:template match="/">
<xsl:variable name="Ixml">
<map href="A.xml">
<topic href="1.xml"/>
<topic href="2.xml"/>
<map href="C.xml">
<topic href="3.xml">
<topic href="4.xml"/>
</topic>
</map>
<map href="B.xml">
<topic href="5.xml">
<topic href="6.xml"/>
<topic href="7.xml"/>
<topic href="8.xml"/>
<topic href="4.xml"/>
<topic href="9.xml"/>
<topic href="10.xml"/>
<topic href="11.xml">
<topic href="4.xml"/>
</topic>
<topic href="12.xml"/>
</topic>
</map>
</map>
</xsl:variable>
<xsl:variable name="Oxml">
<xsl:apply-templates select="$Ixml" mode="removeDuplicates"/>
</xsl:variable>
<xsl:copy-of select="$Oxml"/>
</xsl:template>
<xsl:template match="node() | @*" mode="removeDuplicates">
<xsl:copy>
<xsl:apply-templates select="node() | @*" mode="removeDuplicates"/>
</xsl:copy>
</xsl:template>
<xsl:template match="topic[@href][not(. is key('removeDuplicate', @href)[1])]" mode="removeDuplicates"/>
</xsl:stylesheet>
可能的重复我不知道为什么需要在这里使用变量,但是您应该能够在Oxml
的变量声明中执行
。还要注意,XSLT 2中基于XSLT 1的
可以更容易地表示为
@MartinHonnen谢谢!这很有帮助。根据你的建议,我用更新的xslt更新了这个问题。请看一看,让我知道更新的xslt是否可以微调?如果您有其他模式也需要身份转换,您可以使用mode=“#all”
在xsl:template
和mode=“#current”上编写该模板
在内部的应用模板上。我不确定您为什么需要在这里使用变量,但您应该能够在Oxml的变量声明中执行
。另外请注意,XSLT 2中基于XSLT 1的
可以更容易地表示为
@MartinHonnen谢谢!这很有帮助。根据你的建议,我用更新的xslt更新了这个问题。你能看一下并告诉我更新后的xslt是否可以微调吗?如果你有其他模式也需要身份转换,你可以在xsl:template
上使用mode=“#all”
和mode=“#current”
在内部apply templates
上编写该模板。
<map href="A.xml">
<topic href="1.xml"/>
<topic href="2.xml"/>
<map href="C.xml">
<topic href="3.xml">
<topic href="4.xml"/>
</topic>
</map>
<map href="B.xml">
<topic href="5.xml">
<topic href="6.xml"/>
<topic href="7.xml"/>
<topic href="8.xml"/>
<topic href="9.xml"/>
<topic href="10.xml"/>
<topic href="11.xml"/>
<topic href="12.xml"/>
</topic>
</map>
</map>
<xsl:key name="removeDuplicate" match="topic" use="@href"/>
<xsl:template match="/">
<xsl:variable name="Ixml">
<map href="A.xml">
<topic href="1.xml"/>
<topic href="2.xml"/>
<map href="C.xml">
<topic href="3.xml">
<topic href="4.xml"/>
</topic>
</map>
<map href="B.xml">
<topic href="5.xml">
<topic href="6.xml"/>
<topic href="7.xml"/>
<topic href="8.xml"/>
<topic href="4.xml"/>
<topic href="9.xml"/>
<topic href="10.xml"/>
<topic href="11.xml">
<topic href="4.xml"/>
</topic>
<topic href="12.xml"/>
</topic>
</map>
</map>
</xsl:variable>
<xsl:variable name="Oxml">
<xsl:apply-templates select="$Ixml" mode="removeDuplicates"/>
</xsl:variable>
<xsl:copy-of select="$Oxml"/>
</xsl:template>
<xsl:template match="node() | @*" mode="removeDuplicates">
<xsl:copy>
<xsl:apply-templates select="node() | @*" mode="removeDuplicates"/>
</xsl:copy>
</xsl:template>
<xsl:template match="topic[@href][not(. is key('removeDuplicate', @href)[1])]" mode="removeDuplicates"/>
</xsl:stylesheet>