Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在XSLT中指定用于删除空标记的异常_Xslt - Fatal编程技术网

在XSLT中指定用于删除空标记的异常

在XSLT中指定用于删除空标记的异常,xslt,Xslt,我们有下面的XSLT,它删除所有级别上的空标记 我们还想指定一些例外情况。它可以是要避免的特定标记,也可以是要避免的参数化标记列表。例如,如果我们遇到一个名为SpecialTag的标记,那么应该允许该标记为空。标签下面的所有东西仍然可以修剪。可能吗 <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform

我们有下面的XSLT,它删除所有级别上的空标记

我们还想指定一些例外情况。它可以是要避免的特定标记,也可以是要避免的参数化标记列表。例如,如果我们遇到一个名为
SpecialTag
的标记,那么应该允许该标记为空。标签下面的所有东西仍然可以修剪。可能吗

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="@*|node()">
  <xsl:copy>
   <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="*[not(descendant-or-self::*[text()[normalize-space()] | @*])]"/>

</xsl:stylesheet>

这里有两种方法。根据您的描述,这涉及到与元素名称列表进行比较

XSLT1.0 XSLT1.0的特性集更为有限——在这里特别影响我们,我们无法与模板
match
表达式中的变量进行比较。因此,我们采取直接的暴力方法

<!-- Test first that the name doesn't match anything we want to keep.
    Just add more conditions as needed.
    Put the main test at the end (that checks for attributes or text). -->
<xsl:template match="*
    [local-name() != 'SpecialTag']
    [local-name() != 'OtherSpecialTag']
    [local-name() != 'ThirdSpecialTag']
    ['Add other conditions here.']
    [not(descendant-or-self::*[text()[normalize-space()] | @*])]
    "/>
<xsl:variable name="Specials">
    <item>SpecialTag</item>
    <item>OtherSpecialTag</item>
    <item>ThirdSpecialTag</item>
    <!-- Add other tag names here. -->
    <item>...</item>
</xsl:variable>

<!-- Test first that the name doesn't match anything we want to keep.
    Note the syntax - we say `not()` rather than using `!=`, 
    because `!=` evaluates to true if *any* <item> value doesn't match
    the name of the current context element.
    Meanwhile, `not(... = ...)` only evaluates to true if *all* of the
    <item> values don't match the name of the current context element. 
    Put the main test at the end (that checks for attributes or text). -->
<xsl:template match="*
    [not(local-name() = $Specials/item)]
    [not(descendant-or-self::*[text()[normalize-space()] | @*])]
    "/>
注 如果元素没有显式前缀,则函数
name()
local-name()
将生成相同的值。如果它们确实有前缀,
name()
包含前缀,而
local-name()
不包含前缀。相应地调整上面的示例代码以匹配您的用例