Hi I had performed a transformation which drops a tag if it is null.
你好,我执行了一个转换,如果它是空的,它会删除一个标签。
I wanted to check whether my transformation is working fine, so instead of checking it manually, I wrote one more XSLT code which just checks the presence of that particular tag in the OUTPUT XML, if it is null, then the second XSLT should output a text "FOUND". (I don't actually need some XML kind of output but I am just using XSLT for searching.)
我想检查我的转换是否正常工作,所以我不再手工检查它,而是编写了另外一个XSLT代码,它只检查输出XML中特定标记的存在,如果它是null,那么第二个XSLT应该输出一个“find”文本。(实际上我不需要XML类型的输出,但我只是使用XSLT进行搜索。)
When I tried with this XSL code ::
当我尝试使用这个XSL代码时::
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/SiebelMessage//SuppressCalendar[.!='']">
FOUND
</xsl:template>
</xsl:stylesheet>
It outputs all the TEXT DATA that is present in the XML file,
它输出XML文件中所有的文本数据,
to avoid that, I had to write this code::
为了避免这种情况,我必须编写以下代码:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/SiebelMessage//SuppressCalendar[.!='']">
FOUND
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
why did the former code outputs TEXT, why should I insist XSL to ignore all other text? is that the behavior of all XML parsers or only of my own (I am using msxml parser).
为什么以前的代码输出文本,为什么我要坚持XSL忽略所有其他文本?是所有XML解析器的行为,还是我自己的行为(我正在使用msxml解析器)。
2 个解决方案
#1
128
why did the former code outputs TEXT, why should I insist XSL to ignore all other text? is that the behavior of all XML parsers or only of my own
为什么以前的代码输出文本,为什么我要坚持XSL忽略所有其他文本?这是所有XML解析器的行为,还是我自己的行为
You are discovering one of the most fundamental XSLT features as specified in the Specification: the built-in templates of XSLT.
您正在发现规范中指定的最基本的XSLT特性之一:XSLT内置模板。
From the Spec:
从规范:
There is a built-in template rule to allow recursive processing to continue in the absence of a successful pattern match by an explicit template rule in the stylesheet. This template rule applies to both element nodes and the root node. The following shows the equivalent of the built-in template rule:
有一个内置的模板规则,允许在没有通过样式表中的显式模板规则成功匹配模式的情况下继续递归处理。这个模板规则适用于元素节点和根节点。下面显示了与内置模板规则等价的内容:
<xsl:template match="*|/"> <xsl:apply-templates/> </xsl:template>
There is also a built-in template rule for each mode, which allows recursive processing to continue in the same mode in the absence of a successful pattern match by an explicit template rule in the stylesheet. This template rule applies to both element nodes and the root node. The following shows the equivalent of the built-in template rule for mode m.
对于每个模式,也有一个内置的模板规则,它允许在样式表中一个显式模板规则没有成功模式匹配的情况下,递归处理可以在相同的模式下继续进行。这个模板规则适用于元素节点和根节点。下面显示了模式m的内置模板规则的等效项。
<xsl:template match="*|/" mode="m"> <xsl:apply-templates mode="m"/> </xsl:template>
There is also a built-in template rule for text and attribute nodes that copies text through:
对于通过以下方式复制文本的文本和属性节点,也有一个内置的模板规则:
<xsl:template match="text()|@*"> <xsl:value-of select="."/> </xsl:template>
The built-in template rule for processing instructions and comments is to do nothing.
处理指令和注释的内置模板规则是什么都不做。
<xsl:template match="processing-instruction()|comment()"/>
The built-in template rule for namespace nodes is also to do nothing. There is no pattern that can match a namespace node; so, the built-in template rule is the only template rule that is applied for namespace nodes.
命名空间节点的内置模板规则也是不做任何事情。没有模式可以匹配名称空间节点;因此,内置模板规则是唯一应用于命名空间节点的模板规则。
The built-in template rules are treated as if they were imported implicitly before the stylesheet and so have lower import precedence than all other template rules. Thus, the author can override a built-in template rule by including an explicit template rule.
内置的模板规则被视为是在样式表之前隐式导入的,因此比所有其他模板规则具有更低的导入优先级。因此,作者可以通过包含显式模板规则来覆盖内置模板规则。
So, the reported behavior is the result of the application of the built-in templates -- the 1st and 2nd of all three of them.
因此,报告的行为是内置模板应用程序的结果——这三个模板中的第一个和第二个。
It is a good XSLT design pattern to override the built-in templates with your own that will issue an error message whenever called so that the programmer immediately knows his transformation is "leaking":
这是一个很好的XSLT设计模式,可以用您自己的模板来覆盖内置的模板,这样就可以在调用时发出错误消息,以便程序员立即知道他的转换是“泄漏”的:
For example, if there is this XML document:
例如,如果有这个XML文档:
<a>
<b>
<c>Don't want to see this</c>
</b>
</a>
and it is processed with this transformation:
这个变换是这样处理的:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="a|b">
<xsl:copy>
<xsl:attribute name="name">
<xsl:value-of select="name()"/>
</xsl:attribute>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
the result is:
其结果是:
<a name="a">
<b name="b">Don't want to see this</b>
</a>
and the programmer will be greatly confused how the unwanted text appeared.
程序员会非常困惑,不需要的文本是如何出现的。
However, just adding this catch-all template
helps avoid any such confusion and catch errors immediately:
但是,只要添加这个“一网打尽”的模板,就可以避免这种混淆,并立即捕获错误:
<xsl:template match="*">
<xsl:message terminate="no">
WARNING: Unmatched element: <xsl:value-of select="name()"/>
</xsl:message>
<xsl:apply-templates/>
</xsl:template>
Now, besides the confusing output the programmer gets a warning that explains the problem immediately:
现在,除了混乱的输出,程序员得到了一个警告,立即解释了问题:
WARNING: Unmatched element: c
#2
13
There are several built in template rules in XSL, one of which is this:
在XSL中有几个内置的模板规则,其中之一是:
<xsl:template match="text()|@*">
<xsl:value-of select="."/>
</xsl:template>
It outputs text.
它输出文本。
#1
128
why did the former code outputs TEXT, why should I insist XSL to ignore all other text? is that the behavior of all XML parsers or only of my own
为什么以前的代码输出文本,为什么我要坚持XSL忽略所有其他文本?这是所有XML解析器的行为,还是我自己的行为
You are discovering one of the most fundamental XSLT features as specified in the Specification: the built-in templates of XSLT.
您正在发现规范中指定的最基本的XSLT特性之一:XSLT内置模板。
From the Spec:
从规范:
There is a built-in template rule to allow recursive processing to continue in the absence of a successful pattern match by an explicit template rule in the stylesheet. This template rule applies to both element nodes and the root node. The following shows the equivalent of the built-in template rule:
有一个内置的模板规则,允许在没有通过样式表中的显式模板规则成功匹配模式的情况下继续递归处理。这个模板规则适用于元素节点和根节点。下面显示了与内置模板规则等价的内容:
<xsl:template match="*|/"> <xsl:apply-templates/> </xsl:template>
There is also a built-in template rule for each mode, which allows recursive processing to continue in the same mode in the absence of a successful pattern match by an explicit template rule in the stylesheet. This template rule applies to both element nodes and the root node. The following shows the equivalent of the built-in template rule for mode m.
对于每个模式,也有一个内置的模板规则,它允许在样式表中一个显式模板规则没有成功模式匹配的情况下,递归处理可以在相同的模式下继续进行。这个模板规则适用于元素节点和根节点。下面显示了模式m的内置模板规则的等效项。
<xsl:template match="*|/" mode="m"> <xsl:apply-templates mode="m"/> </xsl:template>
There is also a built-in template rule for text and attribute nodes that copies text through:
对于通过以下方式复制文本的文本和属性节点,也有一个内置的模板规则:
<xsl:template match="text()|@*"> <xsl:value-of select="."/> </xsl:template>
The built-in template rule for processing instructions and comments is to do nothing.
处理指令和注释的内置模板规则是什么都不做。
<xsl:template match="processing-instruction()|comment()"/>
The built-in template rule for namespace nodes is also to do nothing. There is no pattern that can match a namespace node; so, the built-in template rule is the only template rule that is applied for namespace nodes.
命名空间节点的内置模板规则也是不做任何事情。没有模式可以匹配名称空间节点;因此,内置模板规则是唯一应用于命名空间节点的模板规则。
The built-in template rules are treated as if they were imported implicitly before the stylesheet and so have lower import precedence than all other template rules. Thus, the author can override a built-in template rule by including an explicit template rule.
内置的模板规则被视为是在样式表之前隐式导入的,因此比所有其他模板规则具有更低的导入优先级。因此,作者可以通过包含显式模板规则来覆盖内置模板规则。
So, the reported behavior is the result of the application of the built-in templates -- the 1st and 2nd of all three of them.
因此,报告的行为是内置模板应用程序的结果——这三个模板中的第一个和第二个。
It is a good XSLT design pattern to override the built-in templates with your own that will issue an error message whenever called so that the programmer immediately knows his transformation is "leaking":
这是一个很好的XSLT设计模式,可以用您自己的模板来覆盖内置的模板,这样就可以在调用时发出错误消息,以便程序员立即知道他的转换是“泄漏”的:
For example, if there is this XML document:
例如,如果有这个XML文档:
<a>
<b>
<c>Don't want to see this</c>
</b>
</a>
and it is processed with this transformation:
这个变换是这样处理的:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="a|b">
<xsl:copy>
<xsl:attribute name="name">
<xsl:value-of select="name()"/>
</xsl:attribute>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
the result is:
其结果是:
<a name="a">
<b name="b">Don't want to see this</b>
</a>
and the programmer will be greatly confused how the unwanted text appeared.
程序员会非常困惑,不需要的文本是如何出现的。
However, just adding this catch-all template
helps avoid any such confusion and catch errors immediately:
但是,只要添加这个“一网打尽”的模板,就可以避免这种混淆,并立即捕获错误:
<xsl:template match="*">
<xsl:message terminate="no">
WARNING: Unmatched element: <xsl:value-of select="name()"/>
</xsl:message>
<xsl:apply-templates/>
</xsl:template>
Now, besides the confusing output the programmer gets a warning that explains the problem immediately:
现在,除了混乱的输出,程序员得到了一个警告,立即解释了问题:
WARNING: Unmatched element: c
#2
13
There are several built in template rules in XSL, one of which is this:
在XSL中有几个内置的模板规则,其中之一是:
<xsl:template match="text()|@*">
<xsl:value-of select="."/>
</xsl:template>
It outputs text.
它输出文本。