XSLT 为相同 XSL 的相同输入 XML 获取两个不同的输出 [英] XSLT Getting two different output for same input XML for same XSL
问题描述
我一直在在线工具 [XSLT 1.0 处理器] 中尝试我的 XSLT 代码:
I have been trying my XSLT code in the online tool [XSLT 1.0 processor]:
http://www.freeformatter.com/xsl-transformer.html
最近,我不得不使用xs:dateTime
,因此开始使用使用XSLT 2.0 处理器的工具,http://xsltransform.net/
Recently, I had to make use of xs:dateTime
and hence started using the tool that uses XSLT 2.0 processor,
http://xsltransform.net/
现在,当我试图解决一个问题时,我发现我在这两个处理器中为相同的输入 XML 获得了不同的输出.此处发布的代码不是我正在处理的真实代码;这是为了模拟我面临的奇怪输出.
Now, when i was trying to solve a problem, i see that i get different output for the same input XML in these two processors. The code posted here is not the real code i am working on; this is to simulate the weird output i faced.
XML:
<?xml version="1.0" encoding="UTF-8"?>
<items>
<book>
<title></title>
</book>
<phone>apple</phone>
</items>
XSLT:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8"
indent="yes" />
<xsl:template match="/">
<items><xsl:apply-templates select="items/*" /></items>
</xsl:template>
<!-- ignore empty elements -->
<xsl:template match="*[not(normalize-space())]" />
<xsl:template match="book">
<newbook><xsl:apply-templates /></newbook>
</xsl:template>
<xsl:template match="title">
<newtitle><xsl:apply-templates /></newtitle>
</xsl:template>
<xsl:template match="phone">
<newphone><xsl:apply-templates /></newphone>
</xsl:template>
</xsl:stylesheet>
来自 http://xsltransform.net/
的输出: [输出 1]
Output from http://xsltransform.net/
: [output 1]
<?xml version="1.0" encoding="UTF-8"?>
<items>
<newbook>
<newtitle/>
</newbook>
<newphone>apple</newphone>
</items>
来自 http://www.freeformatter.com/xsl-transformer.html
的输出: [输出 2]
Output from http://www.freeformatter.com/xsl-transformer.html
: [output 2]
<?xml version="1.0" encoding="UTF-8"?>
<items>
<newphone>apple</newphone>
</items>
预期输出 XML 是 output 2
.
Expected output XML is output 2
.
知道为什么会有这种不同的输出行为吗?
Any idea why this different output behavior?
注意:我查看了另一个 SO 问题:为同一个 XSL 文件生成两个不同的输出?,但那是不同的.
note: I looked at another SO question: Generating two different outputs for the same XSL file?, but that is different.
编辑
为了更清楚地说明哪个工具产生了哪个输出:
To add more clarity on which tool produced which output:
http://xsltransform.net/
产生 output 1
http://www.freeformatter.com/xsl-transformer.html
产生 output 2
更新[02/10/2014] - 关于解决方案
由于 Jim Garrison 首先提供了正确答案,因此将其标记为答案.然而,还有其他重要的点,正如其他人所指出的,所以我在这里总结一下.
Since the correct answer was first provided by Jim Garrison, that's marked as answer. However, there are other important points as well, as pointed out by others, Hence I am wrapping up all here.
<xsl:template match="*[not(normalize-space())]" />
上述模板消除了 XSLT1 和 XSLT2 中的空节点.因此输出 2 是正确的
The above template eliminates the empty nodes in both XSLT1 and XSLT2. Hence output 2 is correct
*从工具中获取输出 1 的原因 - http://xsltransform.net/ :*
*Reason for getting output 1 from the tool - http://xsltransform.net/ :*
- 正如@michael.hor257k 指出的,有两个模板与
元素匹配. - 该工具使用的是 Saxon-HE 9.5.1.3,输出 1 行为可能是最新维护版本中修复的错误 [@Michael Kay 在他的回答中解释得很好]立>
- 正如@Ian Roberts 在他的回答中提到的,有一个分配给不同类型模板的默认优先级概念.将模板语法更改为:
<xsl:template match="*[not(normalize-space())]" priority="2"/>
无论 Saxon 版本如何,这两种工具都会产生相同的输出,因为我们明确定义了模板的优先级.
- As @michael.hor257k pointed out, there are two templates that matches the
<book>
element. - The tool was using Saxon-HE 9.5.1.3, and the output 1 behavior was probably a bug that was fixed in the latest maintenance release [@Michael Kay explained well in his answer]
- As @Ian Roberts mentioned in his answer, there is a concept of default priorities that are assigned to different types of templates.
Changing the template syntax to:
<xsl:template match="*[not(normalize-space())]" priority="2"/>
yields the same output in both the tools regardless of Saxon version, because we are explicitly defining the priority to the template.
推荐答案
输出 2 是正确的.我在 Oxygen/XML 中运行了您的示例,同时使用了 XSLT1 和 XSLT2,并得到了正确的结果.您的模板
Output 2 is correct. I ran your example in Oxygen/XML, using both XSLT1 and XSLT2 and got the correct results for both. Your template
<xsl:template match="*[not(normalize-space())]" />
消除 XSLT1 和 XSLT2 中的空节点.
eliminates the empty nodes in both XSLT1 and XSLT2.
因此,唯一可能的结论是生成输出 1 的在线工具存在错误.
Therefore the only possible conclusion is that the online tool that produced your Output 1 has a bug.
这篇关于XSLT 为相同 XSL 的相同输入 XML 获取两个不同的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!