为什么 XSLT 默认输出所有文本?
您好,我执行了一个转换,如果标签为空,则删除该标签。
我想检查我的转换是否正常工作,因此我没有手动检查它,而是编写了另一个 XSLT 代码,该代码仅检查 OUTPUT XML 中是否存在该特定标记,如果它为 null,则第二个 XSLT 应输出文本“找到”。 (我实际上不需要某种 XML 类型的输出,但我只是使用 XSLT 进行搜索。)
当我尝试使用此 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>
它输出 XML 文件中存在的所有文本数据时,
为了避免这种情况,我必须编写这段代码::
<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>
为什么前面的代码输出文本,为什么我应该坚持 XSL 忽略所有其他文本?这是所有 XML 解析器的行为还是仅是我自己的行为(我正在使用 msxml 解析器)。
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.)
When I tried with this XSL code ::
<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,
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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在发现规范中指定的最基本的 XSLT 功能之一:XSLT 内置模板。
来自规范:
因此,报告的行为是应用内置模板的结果——所有三个模板中的第一个和第二个。
用自己的模板覆盖内置模板是一个很好的 XSLT 设计模式,每当调用时都会发出错误消息,以便程序员立即知道他的转换正在“泄漏”:
例如,如果有这样的XML文档:
并且用这种转换处理它:
结果是:
程序员会非常困惑不需要的文本是如何出现的。
但是,只需添加这个
包罗万象的模板
就可以帮助避免任何此类混乱并立即捕获错误:现在,除了令人困惑的输出之外,程序员收到一条警告,立即解释了问题:
Michael Kay 对 XSLT 3.0 的后期添加
在 XSLT 3.0 中,您可以指定后备行为,而不是添加包罗万象的模板规则
xsl:mode
声明。例如,
会导致跳过所有不匹配的节点(包括文本节点),而< ;xsl:mode on-no-match="fail"/>
将不匹配视为错误,
会产生警告。You are discovering one of the most fundamental XSLT features as specified in the Specification: the built-in templates of XSLT.
From the Spec:
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":
For example, if there is this XML document:
and it is processed with this transformation:
the result is:
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:Now, besides the confusing output the programmer gets a warning that explains the problem immediately:
Later Addition by Michael Kay for XSLT 3.0
In XSLT 3.0, rather than adding a catch-all template rule, you can specify the fallback behaviour on an
xsl:mode
declaration. For example,<xsl:mode on-no-match="shallow-skip"/>
causes all nodes that are not matched (including text nodes) to be skipped, while<xsl:mode on-no-match="fail"/>
treats a no-match as an error, and<xsl:mode warning-on-no-match="true"/>
results in a warning.XSL 中有多个内置模板规则,其中之一是:
输出文本。
There are several built in template rules in XSL, one of which is this:
It outputs text.