XSLT Getting two different output for same input XML for same XSL

前端 未结 4 1388
囚心锁ツ
囚心锁ツ 2021-01-20 04:22

I have been trying my XSLT code in the online tool [XSLT 1.0 processor]:

http://www.freeformatter.com/xsl-transformer.html

Recently, I had to make

相关标签:
4条回答
  • 2021-01-20 04:22

    Output 2 is definitely the correct one according to both the 1.0 and 2.0 XSLT specs for the stylesheet you give in the question. However you say this is simplified when compared to your real stylesheet.

    The thing that makes it work is the default priorities that are assigned to different types of templates. These are the same in both XSLT versions, and a match pattern that involves a predicate will overrule one that is just a single element name test.

    However a match that involves hierarchy (e.g. match="items/book") or anything more complex than just a single element name will get the same priority as the *[....] rule by default - 0.5. If you've got any of these kinds of patterns in your real XSLT then you need to add an explicit priority higher than 0.5 to the "blank-suppressor" template.

    <xsl:template match="*[not(normalize-space())]" priority="2"/>
    
    0 讨论(0)
  • 2021-01-20 04:33

    Note that an empty <book> element matches two of your templates:

    <xsl:template match="*[not(normalize-space())]" />
    

    and:

    <xsl:template match="book">
      <newbook><xsl:apply-templates /></newbook>
    </xsl:template>
    

    The same thing applies to an empty <title> element. Such conflicts must be resolved by the XSLT processor, using the priorities specified in the XSLT specification.

    The result you report from http://xsltransform.net/ is generated by Saxon 9.5.1 - an XSLT 2.0 engine. You will get a different result (on the same site) if you switch to an XSLT 1.0 engine (Saxon 6.5 or Xalan 2.7.1).

    I don't know if the conflict resolution rules have changed in XSLT 2.0 compared to XSLT 1.0; I am getting a different result myself when using Saxon 8.9 - also an XSLT 2.0 processor. Perhaps we will be fortunate enough to hear from Michael Kay regarding this difference (I have added a Saxon tag to your question to get his attention)..

    0 讨论(0)
  • 2021-01-20 04:44

    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())]" />
    

    eliminates the empty nodes in both XSLT1 and XSLT2.

    Therefore the only possible conclusion is that the online tool that produced your Output 1 has a bug.

    0 讨论(0)
  • 2021-01-20 04:48

    I get the correct result running with Saxon-EE 9.5.1.4; the xsltransform site is running Saxon-HE 9.5.1.3. It's probably a bug that we fixed in the latest maintenance release; I'll do some chasing to see if I can finger it.

    0 讨论(0)
提交回复
热议问题