首页 > 解决方案 > 如何放置“和”条件以从 xslt 中删除 xml 元素

问题描述

我是 XSLT 的新手,并且有一个条件可以根据 XSLT 中应用的条件从 XML 文件中删除一个元素。我必须根据同一属性的 2 个条件删除 en 元素。

这是我的虚拟代码:

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


<xsl:template match="cdm:attributeList/cdm:attribute[cdm:attributeName = 'format'] and cdm:attributeList/cdm:attribute[cdm:attributeValue = 'XYZ']" />

运行 XSLT 文件时出现以下错误: 1. 额外的非法标记:'and' 2. xsl:output 元素上不允许使用“exclude-result-prefixes”属性!

有人可以帮我解决这个问题吗?谢谢。

标签: xslt-1.0

解决方案


模板匹配条件不正确,and匹配模板时不允许操作符。您需要修改模板匹配条件如下,它对应于所需条件的组合。

<xsl:template match="cdm:attributeList/cdm:attribute[cdm:attributeName = 'format'][cdm:attributeValue = 'XYZ']" />

对于第二个问题exclude-result-prefixes,它是一个属性<xsl:stylesheet>而不是<xsl:output>。因此,如果您不希望输出节点具有cdm前缀,请修改<xsl:stylesheet>如下

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:cdm="http://someurl" exclude-result-prefixes="cdm">

推荐阅读