我有两个看起来像的变量
<xsl:variable name="A">
<asset id="111">[...]</asset>
<asset id="222">[...]</asset>
</xsl:variable>
<xsl:variable name="B">
<asset id="333">[...]</asset>
<asset id="444">[...]</asset>
<asset id="111">[...]</asset>
<asset id="555">[...]</asset>
<asset id="999">[...]</asset>
</xsl:variable>
我希望获取A的每个ID值并检查是否在B中找不到它。如果没有找到,则将当前节点从A复制到新变量中。
<xsl:variable name="Final">
<xsl:for-each select="$A/asset">
<xsl:variable name="presentXML">
<xsl:copy-of select="."/>
</xsl:variable>
<xsl:variable name="presentID">
<xsl:value-of select="./@id"/>
</xsl:variable>
<xsl:for-each select="$B/asset">
<xsl:variable name="pastID">
<xsl:value-of select="./@id"/>
</xsl:variable>
<xsl:choose>
<xsl:when test="$presentID != $pastID">
<xsl:copy-of select="$presentXML"/>
</xsl:when>
<xsl:otherwise>
</xsl:otherwise >
</xsl:choose>
</xsl:for-each>
</xsl:for-each>
</xsl:variable>
如果上面的代码有效,则只有 [...] 会被添加到变量 Final 中。我无法想出一种方法来正确测试
无需迭代。你可以简单地做:
<xsl:copy-of select="$A/asset[not(@id=$B/asset/@id)]"/>
(前提是您的处理器支持 XSLT 2.0 或更高版本。否则您需要首先将变量转换为节点集。))